def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = util.logging.get_logger("project-receiver") # Naive solution, where we continually read data off the socket # until we don't receive any more data, and then return. num_bytes = 0 while True: data = sock.recv(util.MAX_PACKET) if not data: break logger.info("Received %d bytes", len(data)) dest.write(data) num_bytes += len(data) dest.flush() return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = homework5.logging.get_logger("hw5-receiver") # Naive solution, where we continually read data off the socket # until we don't receive any more data, and then return. num_bytes = 0 sequenceNumber = 0 while True: data = sock.recv(homework5.MAX_PACKET) if not data: break header = data[:4] data = data[4:] tempNumber = struct.unpack("i", header)[0] if data[4:] is b'': break logger.info("Received %d bytes", len(data)) if tempNumber > sequenceNumber: sequenceNumber = tempNumber dest.write(data) num_bytes += len(data) dest.flush() sock.send(struct.pack("i", sequenceNumber)) return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ global EXPECTED_SEQUENCE logger = homework5.logging.get_logger("hw5-receiver") num_bytes = 0 while True: try: data = sock.recv(homework5.MAX_PACKET) # Kill the process as soon as there is nothing else to send if not data: break # Gather the packet and retrieve the sequence number and data new_packet = decode_packet(data) header_only = new_packet[0] data_only = new_packet[1] # Check if the packet received is not off if header_only == EXPECTED_SEQUENCE: # If the packet received also contains data, then send an ack if data_only is not None: # Send an Acknowledgement that the data received corresponds # to expected value sock.send(make_packet(EXPECTED_SEQUENCE)) logger.info("Received %d bytes", len(data_only)) dest.write(data_only) num_bytes += len(data_only) dest.flush() # Update the expected sequence if the data that we received # is the one that was expected update_sequence() # If the packet sequence is off, resent the else: sock.send(make_packet(EXPECTED_SEQUENCE)) # If there was a timeout, continue except socket.timeout: continue return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = homework5.logging.get_logger("hw5-receiver") num_bytes = 0 sequence = 0 count = 0 while True: data = sock.recv(homework5.MAX_PACKET) #print("\nRECEIVER DATA[0]->", data[:2]) if data[0] == sequence: sock.send(bytes([sequence])) #print('\n') print("RECEVIER SENT THESE BYTES->", str(data[0])) buff = data[1:] logger.info("Received %d bytes", len(buff)) #print('\n') dest.write(buff) count = 0 num_bytes += len(buff) dest.flush() if sequence == 255: sequence = 0 else: sequence+=1 continue elif data[:2] == FIN: sock.send(FIN) # print("RECV CLOSED SUCCESSFULLY") sock.close() break if data[0] != sequence: # sequence-=1 # if count ==0: sock.send(bytes([data[0]])) print("WRONG SEQUENCE -- RECEVIER GOT THESE BYTES->", str(data[0])) print("looking for these bytes->",sequence) # count+=1 # sock.send(bytes([data[0]])) # print("DATA was dropped I am RECIEVER and still looking for sequence",str(bytes([sequence]))) continue break return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = homework5.logging.get_logger("reliable-receiver") # Naive solution, where we continually read data off the socket # until we don't receive any more data, and then return. reply = packet_listening(sock) acknowledgement = 1 num_bytes = 0 reply1 = [] reply1 = list(reply) total = 0 for x in range(1): reply1[0] = random.randint(1,5) reply1[1] = reply1[1] + 1 reply1[2] = reply1[2] + 1 second_packet = make_packet(reply1[0], reply1[1], reply1[2], reply1[3]) sock.send(second_packet) a = 2 while(a == 2): for x in range(1): reply1[0] = random.randint(1,5) reply1[1] = reply1[1] + 1 reply1[2] = reply1[2] + 1 second_packet = make_packet(reply1[0], reply1[1], reply1[2], reply1[3]) data1 = sock.recv(5000) dlen = len(data1) - 8 data = data1[:dlen] checksum = struct.unpack("L", data1[dlen:]) checkchecksum = binascii.crc32(data) if data and checkchecksum in checksum: sock.send(second_packet) else: sock.close() break logger.info("Received %d bytes", len(data)) dest.write(data) num_bytes += len(data) dest.flush() return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ # Naive solution, where we continually read data off the socket # until we don't receive any more data, and then return. num_bytes = 0 received_chunks = [] expected_chunk = 0 last_ack = 0 while True: # print('Received: ', received_chunks) data = sock.recv(homework5.MAX_PACKET) if not data: break # print("Received bytes: ", data[0], " -> ", len(data)) # The chunk was not received yet # print('expected_chunk: ', expected_chunk, 'data[0]; ', data[0]) if data[0] not in received_chunks and data[0] == expected_chunk: expected_chunk = expected_chunk + 1 expected_chunk = expected_chunk % 256 received_chunks.append(data[0]) if len(received_chunks) > 240: received_chunks.pop(0) dest.write(data[1:]) num_bytes += len(data[1:]) dest.flush() # send ACT # print('20. sending ack: ', data[0]) sock.send(bytes([data[0]])) last_ack = data[0] # ack lost, resent elif data[0] in received_chunks and rcv_bef(data[0], expected_chunk): # print('21. sending ack: ', data[0]) sock.send(bytes([data[0]])) # Chunk was received before else: # print('22. sending ack: ', last_ack) sock.send(bytes([last_ack])) return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = homework5.logging.get_logger("hw5-receiver") call = 0 num_bytes = 0 while True: rcvpkt = sock.recv(homework5.MAX_PACKET + 3) if not rcvpkt: break seqNum, checksum = extract_header(rcvpkt) corrupt = is_corrupt(rcvpkt) # Verify received data packet if not corrupt and seqNum == call: # Valid packet, send correct ACK sndpkt = make_ACK(seqNum) sock.send(sndpkt) data = extract_data(rcvpkt) dest.write(data) num_bytes += len(data) dest.flush() call = 1 - call elif corrupt or seqNum != call: # Invalid packet, must send wrong ACK sndpkt = make_ACK(seqNum) sock.send(sndpkt) return num_bytes
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ # Naive solution, where we continually read data off the socket # until we don't receive any more data, and then return. number_of_bytes = 0 previous_header = [] # continuous loop for receiving data while True: # gets packet packets = sock.recv(homework5.MAX_PACKET) # if packet == null, exit out of loop if not packets: break else: # take out the header from the packet header = packets[0:4] # send it back to the sender as confirmation sock.send(header) # if header is same as prev header, return to top of loop if checker(previous_header, header): continue length_of_packet = len(packets) # update total bytes received number_of_bytes += length_of_packet - 4 # update prev header previous_header = header # write it into destination dest.write(packets[4:length_of_packet]) dest.flush() return number_of_bytes
def _send(self, strm: BufferedIOBase, obj: Any) -> None: b = msgpack.packb(obj, use_bin_type=True) strm.write(struct.pack('<I', len(b))) strm.write(b) strm.flush()
def recv(sock: socket.socket, dest: io.BufferedIOBase) -> int: """ Implementation of the receiving logic for receiving data over a slow, lossy, constrained network. Args: sock -- A socket object, constructed and initialized to communicate over a simulated lossy network. Return: The number of bytes written to the destination. """ logger = miniproject3.logging.get_logger("mp3-receiver") # rdt 2.1 num_bytes = 0 # set state to 0 state = '00000' while True: data = sock.recv(miniproject3.MAX_PACKET) if not data: break # get the sequence number sequence = data[0:5].decode() print('Receiver: SEQ = %s' % sequence) # show the sequence sock.send(sequence.encode()) # ACK # state 0: wait for 0 from below if state == '00000': while True: # has_seq0() if sequence == state: # change state print('change state to 1') state = '00001' # change state to 1 # deliver logger.info("Received %d bytes", len(data)) dest.write(data[5:]) num_bytes += len(data) dest.flush() break # has_seq1(): duplicate else: break # state 1: wait for 1 from below # if state == '00001': else: while True: # has_seq1() if sequence == state: # change state print('change state to 0') state = '00000' # change state to 0 # deliver logger.info("Received %d bytes", len(data)) dest.write(data[5:]) num_bytes += len(data) dest.flush() break # has_seq0(): wait for the next packet else: break return num_bytes