def receive_gbn(sock, sent): sock.settimeout(0.5 * 50) acknowledged = len(sent) count = 0 while count != acknowledged: try: for x in range(len(sent)): ACK, senderaddr = udt.recv(sock) ack, data = packet.extract(ACK) print("Confirm seq#: ", ack, "\n") acknowledged += 1 except socket.timeout: print("Resending") for x in sent: udt.send(x, sock, RECEIVER_ADDR) return
def receive_gbn(sock): global mutex global base global send_timer # waits to recive ACK's while True: pkt, _ = udt.recv(sock) ack, _ = packet.extract(pkt) print('Got ACK :', ack) if (ack >= base): mutex.acquire() base = ack + 1 print('Base updated :', base) send_timer.stop() mutex.release()
def receive_snw(sock): #Mod rcvr SNW by Jennifer endStr = '' _seq = -1 while True: pkt, senderaddr = udt.recv(sock) seq, data = packet.extract(pkt) if _seq != seq: _seq = seq endStr = data.decode() #sys.stderr.write("From: {}, Seq# {}\n".format(senderaddr, seq)) #sys.stderr.flush() if endStr == 'END': return sys.stdout.write(endStr) sys.stdout.flush() udt.send(b' ', sock, ('localhost', 9090))
def receive_snw(sock, filename): global ack endStr = '' prevData = '' #Holds previous data incase of packet drop from Receiver to Sender #If data from packet contains 'END', receive stream has ended. while endStr != 'END': #Extracts packet and sender address from socket. pkt, senderaddr = udt.recv(sock) #Extracts sequence number and data from packet. seq, data = packet.extract(pkt) #If data equals the previous data sent then packet was dropped from Receiver #to sender. Resend previous ack number with data. if data == prevData: print( "Data received is previous data received. Resending previous ack." ) if (ack == 0): pkt = packet.make(ack + 1, data) udt.send(pkt, sock, senderaddr) else: pkt = packet.make(ack - 1, data) udt.send(pkt, sock, senderaddr) #If sequence does not equal ack from Sender then packet dropped causing #misorder. Send the correct sequence from Receiver back to Sender. elif (seq != ack): print("Seq ", seq, " and ack ", ack, " do not match. Waiting for resend.") pkt = packet.make(ack, data) udt.send(pkt, sock, senderaddr) #Sequence matches ack from Sender. #Write data to new file, incrememnt Receiver ack, send back the Sender ack #to acknowledge receieved proper sequenced number. else: print("Seq ", seq, " and ack ", ack, " match. Will write to file.") endStr = data.decode() file = open(filename, "a") file.write(endStr) pkt = packet.make(ack, data) prevData = data if (ack == 0): ack = 1 else: ack = 0 udt.send(pkt, sock, senderaddr)
def receive(sock): global mutex global base global send_timer while True: pkt, _ = udt.recv(sock); ack, _ = packet.extract(pkt); # If we get an ACK for the first in-flight packet print('Got ACK', ack) if (ack >= base): mutex.acquire() base = ack + 1 print('Base updated', base) send_timer.stop() mutex.release()
def receive(sock): global mutex global base global send_timer while True: pkt, _ = sock.recvfrom(PACKET_SIZE) ack, _ = packet.extract(pkt) # If we get an ACK for the first in-flight packet print('Got ACK', ack, ' expected ', base) if (int(ack) >= base): mutex.acquire() base = int(ack) + 1 print('Base updated', base) send_timer.stop() mutex.release()
def receive_gbn(sock): global mutex global base global timer while True: # receive ACKs then extract them pkt, addr = udt.recv(sock) ack, data = packet.extract(pkt) print("Received ack: %s" % ack) if ack >= base: # check if ack is expected ack seq # mutex.acquire() # lock this thread base = ack + 1 # increment base seq print("Updated next sequence #: %s" % base) # update next base seq # timer.stop() mutex.release()
def receive_gbn(sock): endStr = '' global received_packets global count f = open("receiver_bio.txt", "w") while endStr != 'END': pkt, senderaddr = udt.recv(sock) seq, data = packet.extract(pkt) if seq not in received_packets and count == seq: received_packets.append(seq) count += 1 endStr = data.decode() if endStr != "END": f.write(endStr) print("From: ", senderaddr, ", Seq# ", seq, "Data ", endStr, "\n") pkt = packet.make(seq, data) udt.send(pkt, sock, senderaddr)
def receive(sock): global mutex global base global send_timer while True: pkt, _ = udt.recv(sock) ack, _ = packet.extract(pkt) # If we get an ACK for the first packet print('Got ACK', ack) if (ack >= base): mutex.acquire() base = ack + 1 print('Base updated', base) send_timer.stop() mutex.release()
def receive_snw(sock): file_data = [] seq_num = 0 while seq_num > -1: p, addr = udt.recv(sock) seq_num, payload = packet.extract(p) print("Received packet: {}".format(seq_num)) #if seq_num == -1: # break udt.send(packet.make(seq_num, b'ACK'), sock, addr) print("Acked packet: {}\n".format(seq_num)) if seq_num > len(file_data) - 1: file_data.append(payload) return file_data
def receive_snw(sock): #open file for writing f = open("receiver_bio.txt", "w") # Terminal String endStr = '' # Most recent sequence number _seq = -1 # Blocking Loop while True: # Block on socket data pkt, senderaddr = udt.recv(sock) seq, data = packet.extract(pkt) # If data is newer if _seq != seq: # Update last sequence _seq = seq # Parse data and write debugging info to logging stream endStr = data.decode() if endStr != 'END': f.write(endStr) sys.stderr.write("From: {}, Seq# {}\n".format(senderaddr, seq)) sys.stderr.flush() # If string is terminal if endStr == 'END': return # Write socket data to output stream sys.stdout.write(endStr) sys.stdout.flush() # Send null ACK udt.send(b' ', sock, ('localhost', 9090)) f.close()
def receive_gbn(sock): # Terminal String endStr = '' # Most recent sequence number _seq = -1 # Blocking Loop while True: # Block on socket data pkt, senderaddr = udt.recv(sock) seq, data = packet.extract(pkt) sys.stderr.write("Received Seq: {}\n".format(seq)) sys.stderr.flush() # If data is newer by exactly 1 in-order segment if seq == _seq + 1: # or (seq == 0 and _seq != -1): # Update last sequence _seq = seq # Parse data and write debugging info to logging stream endStr = data.decode() sys.stderr.write("From: {}, Seq# {}\n".format(senderaddr, seq)) sys.stderr.flush() # If string is terminal if endStr == 'END': return # Write socket data to output stream sys.stdout.write(endStr) sys.stdout.flush() # Send null ACK ack = packet.make(seq, b' ') udt.send(ack, sock, ('localhost', 9090)) return
def receive(sock): global mutex global base global send_timer print("receive init") print(sock) while True: pack, addr = sock.recvfrom(1024) ack, address2 = packet.extract(pack, False) # If we get an ACK for the first in-flight packet print('Got ACK', ack) if (ack >= base): mutex.acquire() base = ack + 1 print('Base updated', base) send_timer.stop() mutex.release()
def receive_gbn(sock): #global vars for comms with sender global mutex global base global timer #print("in receive") #DEBUG #Check for incoming ACKS while True: pkt,senderAddress = udt.recv(sock); #Address unused ack,ackData = packet.extract(pkt) #Data could be checked for corruption #print("got ack") #DEBUG #Might have multiple ACKS in buffer, empty it out and iterate accordingly if (base <= ack): #print("ack is relevant") #DEBUG mutex.acquire() #Stops sending to update base base = ack+1 #scoot up base on each successful ACK timer.stop() #timeout expired (in a good way) mutex.release() #Lets Sender continue working
def receive_gbn(sock): global base, timer, mutex end = '' try: while end != 'END': pckt, sendAddr = udt.recv(sock) packetSeq, data = packet.extract(pckt) end = data.decode() print("\nPacket From: ", SENDER_ADDR, ", Seq# ", packetSeq, end) if base <= packetSeq: mutex.acquire() base = packetSeq + 1 timer.stop() mutex.release() except IOError: print(IOError) return
def receive_gbn(sock): previous = -1 packets = [] addr_connection = None while True: try: p, addr = udt.recv(sock) seq_num, payload = packet.extract(p) # If this is a new connection, save address and store expected payload length if addr_connection == None and seq_num == 0: addr_connection = addr # if packet received was from the expected sender... if addr == addr_connection: # if packet signals end of transmission, stop listening for packets if seq_num == -1 and payload == b'FIN': print("File received successfully.") break # if sequence number is what we're expecting, send an ack and append packet contents to packet stack if seq_num == previous + 1: udt.send(packet.make(seq_num, b'ACK'), sock, addr) packets.append((seq_num, payload)) previous += 1 print("Received packet: {}".format(seq_num)) # retransmit ack for last-received packet else: if len(packets) > 0: udt.send(packet.make(packets[-1][0], b'ACK'), sock, addr) except ConnectionError as e: print(e) return -1 # because packet list contains sequence numbers, simply form a list return list(dict(packets).values())
def receive_gbn(sock): '''file created to save received packets''' try: file_out = open('receiver_bio.txt', 'a') except IOError: print("File not created") return endStr = '' expected_seq = 0 '''if socket doesnt receive info in 10 seconds, then end communication''' sock.settimeout(10) while endStr != 'END': '''checking if sender continues to transmit data''' try: pkt, senderaddr = udt.recv(sock) except socket.timeout: print('Sender seems inactive......') print('Shutting down receiver.....') break seq, data = packet.extract(pkt) print("Received Packet: ",seq) if seq == expected_seq: '''extracting valid packet contents and appending contents to receiver_bio.txt file''' endStr = data.decode() print("From: ", senderaddr, ", Seq# ", seq, endStr) print("Sending ACK:", expected_seq) pkt = packet.make(expected_seq) udt.send(pkt, sock, senderaddr) file_out.write(endStr) expected_seq = expected_seq + 1 else: ##Sending last valid ACK print("Sending ACK:",expected_seq-1) pkt = packet.make(expected_seq-1) udt.send(pkt,sock,senderaddr) print("End of transmission") file_out.close()
def receive_snw(sock): try: # open file file = open('./files/receiver_bio.txt', 'a') except: # cannot create file print('Could Access location') sys.exit(1) sock.settimeout(5) # socket timer data = '' previous = -1 while data != 'END': try: clientPacket, senderAddress = udt.recv(sock) # client response except: break clientSequence, clientPayload = packet.extract(clientPacket) data = clientPacket.decode() print('Seq#: %i' % clientSequence) if previous != clientSequence: file.write(clientPayload.decode()) # write data to file acknowledgement = str(clientSequence).encode() # generate ack and send udt.send(acknowledgement, sock, senderAddress) previous = clientSequence # update last seen client sequence
def receive(sock, num_packets): global mutex global base global send_timer global process_start_time print('Window state:', base, 'to', min(WINDOW_SIZE, num_packets - base) + base - 1) while True: pkt, _ = udt.recv(sock) ack, _ = packet.extract(pkt) mutex.acquire() print('Got ACK', ack, 'Time', round(time.time() - process_start_time, 3), 'seconds') if (ack >= base): base = ack + 1 print('Window state:', min(base, num_packets - 1), 'to', min(WINDOW_SIZE, num_packets - base) + base - 1) send_timer.stop() mutex.release()
def receive(sock): expected_num = 0 while True: # Get the next packet from the sender pkt, addr = sock.recvfrom(PACKET_SIZE) if not pkt: break seq_num, data = packet.extract(pkt) print('Got packet', seq_num, ' expected ', expected_num) # Send back an ACK if int(seq_num) == expected_num: print('Got expected packet') recieved.append(data) print('Sending ACK', expected_num) pkt = packet.make(expected_num) sock.sendto(pkt, addr) expected_num += 1 else: print('Sending ACK', expected_num - 1) pkt = packet.make(expected_num - 1) sock.sendto(pkt, addr)
def receive(sock, filename): # Open the file for writing try: file = open(filename, 'wb') except IOError: print('Unable to open', filename) return expected_num = 0 while True: # Get the next packet from the sender pkt, addr = sock.recvfrom(BUFFER_SIZE) print("收到数据data", packet) if not pkt: break seq_num, data = packet.extract(pkt, True) if data == '': continue print('Got packet', seq_num) # Send back an ACK if seq_num == expected_num: print('Got expected packet') print('Sending ACK', expected_num) pkt = packet.make(expected_num) if random.randint(0, DROP_PROB) > 0: sock.sendto(pkt, addr) expected_num += 1 file.write(data) else: print('Sending ACK', expected_num - 1) pkt = packet.make(expected_num - 1) if random.randint(0, DROP_PROB) > 0: sock.sendto(pkt, addr) file.close()
def receive_gbn(sock): #global vars for comms with sender global mutex global base global timer #print("in receive") #DEBUG #Check for incoming ACKS while True: pkt, senderAddress = udt.recv(sock) #get packet and address of sender (currently only packet is needed) ack, ackData = packet.extract( pkt ) #Get ACK # and data. data is unused (as there should only be a confirmed ack #) #print("got ack") #DEBUG #For each confirmed ack (that is relevant), move the base up if (base <= ack): print("ack is relevant") #DEBUG mutex.acquire() #Stops sending to update base base = ack + 1 #scoot up base on each successful ACK timer.stop() #stop timer since ack made it back in time mutex.release() #Lets Sender continue working
def send(sock, filename): global mutex global base global window global start_of_program # Open file try: file = open(filename, 'rb') except IOError: print('Unable to open', filename) return # Add all the packets to the buffer packets = [] seq_num = 0 while True: data = file.read(PACKET_SIZE) if not data: break packets.append(packet.make(seq_num, data)) seq_num += 1 num_packets = len(packets) print('There are :', num_packets) next_to_send = 0 base = 0 arr_of_pack = [] # Start the receiver thread _thread.start_new_thread(receive, (sock, )) while base < num_packets: mutex.acquire() # start sending packets if window.contain() < 4 and next_to_send < num_packets: print('Sending packet', next_to_send) arr_of_pack.append(packet.Packeto(packets[next_to_send])) window.add_packet(packet.Packeto(packets[next_to_send])) udt.send(packets[next_to_send], sock, RECEIVER_ADDR) next_to_send = next_to_send + 1 elif window.packet1().stat() == 1 and next_to_send < num_packets: print('Sending packet', next_to_send) arr_of_pack.append(packet.Packeto(packets[next_to_send])) window.add_packet(packet.Packeto(packets[next_to_send])) udt.send(packets[next_to_send], sock, RECEIVER_ADDR) next_to_send = next_to_send + 1 elif next_to_send == num_packets and window.packet1().stat( ) == 1 and window.packet2().stat() == 1 and window.packet3().stat( ) == 1 and window.packet4().stat() == 1: break while window.timer_status() and window.contain() > 3: print('Waiting') mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() if window.contain() < 4: wid = 0 elif window.packet1().stat() == 0 and window.packet1().did_runout(): # timed out print('Time out 1') ack, _ = packet.extract(window.packet1().returnpacket()) udt.send(window.packet1().returnpacket(), sock, RECEIVER_ADDR) window.packet1().starttimer() mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() elif window.contain() > 1 and (window.packet2().stat() == 0 and window.packet2().did_runout()): # timed out print('Time out 2') ack, _ = packet.extract(window.packet2().returnpacket()) udt.send(window.packet2().returnpacket(), sock, RECEIVER_ADDR) window.packet2().starttimer() mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() elif window.contain() > 2 and (window.packet3().stat() == 0 and window.packet3().did_runout()): # timed out print('Time out 3') ack, _ = packet.extract(window.packet3().returnpacket()) udt.send(window.packet1().returnpacket(), sock, RECEIVER_ADDR) window.packet1().starttimer() mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() elif window.contain() > 3 and (window.packet4().stat() == 0 and window.packet4().did_runout()): # timed out print('Time out 4') ack, _ = packet.extract(window.packet4().returnpacket()) udt.send(window.packet4().returnpacket(), sock, RECEIVER_ADDR) window.packet4().starttimer() mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() #sees if it is time to leave while elif window.contain() > 3 and window.packet1().stat( ) == 1 and window.packet2().stat() == 1 and window.packet3().stat( ) == 1 and window.packet4().stat() == 1: ack, _ = packet.extract(window.packet4().returnpacket()) if ack + 1 == num_packets: break else: mutex.release() time.sleep(SLEEP_INTERVAL) mutex.acquire() mutex.release() # Send empty packet as end udt.send(packet.make_empty(), sock, RECEIVER_ADDR) time.sleep(SLEEP_INTERVAL) udt.send(packet.make_empty(), sock, RECEIVER_ADDR) time.sleep(SLEEP_INTERVAL) udt.send(packet.make_empty(), sock, RECEIVER_ADDR) file.close()
def receive(sock): global mutex global base global window varable = True val2 = 0 val3 = 0 val4 = 0 while varable == True: pkt, _ = udt.recv(sock) ack, _ = packet.extract(pkt) print('Got ACK :', ack) mutex.acquire() caught_up_by = 0 packet_number = window.return_packet(ack) if packet_number == 1: window.packet1().acked() window.packet1().endtimer() if window.packet1().stat() == 0: base = base + 1 window.packet1().acked() window.packet1().endtimer() if window.contain() > 1 and window.packet2().stat() == 1: if val2 == 1: caught_up_by = caught_up_by + 1 base = base + val2 val2 = 0 if window.contain() > 2 and window.packet3().stat() == 1: if val3 == 1: caught_up_by = caught_up_by + 1 base = base + val3 val3 = 0 if window.contain() > 3 and window.packet4().stat() == 1: if val4 == 1: caught_up_by = caught_up_by + 1 base = base + val4 val4 = 0 if caught_up_by > 0: print('caught up by: ', caught_up_by) caught_up_by = 0 elif packet_number == 2: print('Got ACK that is ahead by: ', packet_number, ' Packets') if window.packet2().stat() == 0: val2 = 1 window.packet2().acked() window.packet2().endtimer() elif packet_number == 3: print('Got ACK that is ahead by: ', packet_number, ' Packets') if window.packet3().stat() == 0: val3 = 1 window.packet3().acked() window.packet3().endtimer() elif packet_number == 4: print('Got ACK that is ahead by: ', packet_number, ' Packets') if window.packet4().stat() == 0: val4 = 1 window.packet4().acked() window.packet4().endtimer() mutex.release()