def send(content, to): checksum = ip_checksum(content) sv[2]) together = (addr, port) sock = socket(AF_INET, SOCK_DGRAM) sock.bind(together) num=0 filename='file_received'+str(num)+'.txt' fp=open(filename,'w') num=1 while True: message, address = sock.recvfrom(1024) if message=='over': fp.c checksum = message[:2]
header = {} header[0] = 0x45 import checksum header = {} header[0] = 0x45 header[1] = 0x00 header[2] = 0x00 header[3] = 0xe8 header[4] = 0x00 header[5] = 0x00 header[6] = 0x40 header[7] = 0x00 header[8] = 0x40 header[9] = 0x11 header[10] = 0x0 header[11] = 0x0 header[12] = 0x0a header[13] = 0x86 header[14] = 0x33 header[15] = 0xf1 header[16] = 0x0a header[17] = 0x86 header[18] = 0x33 header[19] = 0x76 print("Checksum is: %x" % (checksum.ip_checksum(header, len(header)),)) print("Should be BD92")
seq = 0 t0 = timeit.default_timer() while offset < len(content): if offset + SEGMENT_SIZE > len(content): segment = content[offset:] else: segment = content[offset:offset + SEGMENT_SIZE] offset += SEGMENT_SIZE ack_received = False while not ack_received: if (count == 0): print "first transmit package", seq sock.settimeout(0.8) sock.sendto( ip_checksum(segment) + str(seq) + segment, dest) count = count + 1 elif (count < 5): print "retransmit package ", seq, "for", count, "times" sock.settimeout(0.8) sock.sendto( ip_checksum(segment) + str(seq) + segment, dest) count = count + 1 else: print "I have retransmitted 5 times and I will close" sock.close() try: message, address = sock.recvfrom(1024) except timeout: print "Timeout"
def send(content, to): checksum = ip_checksum(content) sock.sendto(checksum + content, to)
header = {} header[0] = 0x45 import checksum header = {} header[0] = 0x45 header[1] = 0x00 header[2] = 0x00 header[3] = 0xe8 header[4] = 0x00 header[5] = 0x00 header[6] = 0x40 header[7] = 0x00 header[8] = 0x40 header[9] = 0x11 header[10] = 0x0 header[11] = 0x0 header[12] = 0x0a header[13] = 0x86 header[14] = 0x33 header[15] = 0xf1 header[16] = 0x0a header[17] = 0x86 header[18] = 0x33 header[19] = 0x76 print("Checksum is: %x" % (checksum.ip_checksum(header, len(header)), )) print("Should be BD92")
packet_list.append(segment) position = position + 1 break else: segment = content[offset:offset + SEGMENT_SIZE] packet_list.append(segment) position = position + 1 offset += SEGMENT_SIZE sock.settimeout(1.0) i = 0 ack_list = [0] * position while (i < position): sock.sendto( ip_checksum(packet_list[i]) + str(seq) + packet_list[i], dest) print seq seq = (seq + 1) % 5 i = i + 1 all_ack_received = False while not all_ack_received: try: message, address = sock.recvfrom(1024) except timeout: print "Timeout" all_ack_received = True else: checksum = message[:2] ack_seq = message[5] if ip_checksum(message[2:]
together = (addr, port) filename = argv[5] with open(filename) as f: content = f.read() sock = socket(AF_INET, SOCK_DGRAM) sock.bind(together) while True: offset = 0 t0 = timeit.default_timer() while offset < len(content): if offset + SEGMENT_SIZE > len(content): segment = content[offset:] else: segment = content[offset:offset + SEGMENT_SIZE] offset += SEGMENT_SIZE sock.sendto(ip_checksum(segment) + segment, dest) print "total delay is ", timeit.default_timer() - t0 sock.sendto('over', dest) x = int( input( "File transfer successfully finished, do you want to transfer again? Reply by 1(Yes) or 0(NO)" )) if x == 0: break sock.close()
sock = socket(AF_INET, SOCK_DGRAM) sock.bind(together) num=0 filename='file_received'+str(num)+'.py' fp=open(filename,'w') num=1 while True: message, address = sock.recvfrom(1024) if message=='over': fp.close() filename='file_received'+str(num)+'.py' num=num+1 fp=open(filename,'w') continue checksum = message[:2] message=corrupt(message) content = message[2:] if ip_checksum(content) == checksum: send("ACK", address) print "successfully received" fp.write(content) else: print "checksum error, discard the corrupted package" fp.close()
num = 1 while True: message, address = sock.recvfrom(1024) if message == 'over': fp.close() filename = 'file_received' + str(num) + '.py' num = num + 1 expected_seq = 0 fp = open(filename, 'w') continue checksum = message[:2] seq = message[2] seq1 = int(seq) message = corrupt(message) content = message[3:] if (ip_checksum(content) == checksum and seq1 == expected_seq): send("ACK" + seq, address) fp.write(content) expected_seq = (expected_seq + 1) % 5 elif ip_checksum(content) == checksum: print "Out of order, discard!" else: print "package biterror" # negative_seq = str(1 - expecting_seq) #send("ACK" + negative_seq, address) fp.close() sock.close()