from packet import packet import socket, sys # save values needed to talk to host emulator haddr = sys.argv[1] # network host address dport = int(sys.argv[2]) # dest port on host rport = int(sys.argv[3]) # recv port for this app received = sys.argv[4] # filename to be used to record recvd data # try opening the file try: msgfile = open(received, 'a') except IOError: sys.stderr.write("Failed to open file to write data. Stop being terrible at life pls") raise SystemExit # logfile (received packets) # at end call things.close() arrlog = open("arrival.log", "a") # some vars needed for execution expected = 0 # next packet # expected confirmed = None # last confirmed packet sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # socket! sock.bind(('', rport)) # set socket to recv on rport pack_size = 512 # packet size in bytes # let's get this bread! I mean packets. yes, I re-used that joke. while(True): pack, addr = sock.recvfrom(pack_size) # if we can't get a packet if (not pack): break else: packet = packet.parse_udp_data(pack) snum = packet.seq_num arrlog.write(str(snum) + "\n") if (snum == expected): # got the next packet if (packet.type == 2): # EOT packet # send EOT and exit sock.sendto(packet.create_eot(snum).get_udp_data(), (haddr, dport)) break elif (packet.type == 1): # data packet # send ACK, record snum, increment expected sock.sendto(packet.create_ack(snum).get_udp_data(), (haddr, dport)) confirmed = snum expected = confirmed + 1 # deal with new data msgfile.write(packet.data) elif (confirmed): # got wrong packet, send confirmation only of last good packet sock.sendto(packet.create_ack(confirmed).get_udp_data(), (haddr, dport)) arrlog.close()