示例#1
0
# Set up socket
s = socket.socket()
s.connect((host, port))


l = int(s.recv(8).decode('utf_8'))
print(l)
print(type(l))
print('Got length {}'.format(l))
text = []
while l:

    received = s.recv(8).decode('utf_8')
    if received == '':
        break
    received = int(received)
    print('Received {}'.format(received))
    decoded_frame = decoder.decode([received], verbose=True)
    if decoded_frame != None:
        text.append(decoded_frame)
    l -= 1

s.close()

print('\t\t')
# print(text)
text = ''.join(text)
print(text)
with open('output.txt', 'w') as f:
    f.write(text)
示例#2
0
    crc = CRC()
    # data_pkt = namedtuple('data_pkt', 'seq_num frame')
    # ack_pkt = namedtuple('ack_pkt', 'seq_num ack')

    # Communication
    s = socket(AF_INET, SOCK_DGRAM)
    s.bind((hostname, port))
    print("Listening for client requests ... ")
    exp_seq_no = 0
    while 1:
        msg, client_address = s.recvfrom(65535)
        data = pickle.loads(msg)
        got_seq_no, frame = data['seq_num'], data['frame']
        print('Packet Recv seq_no {} '.format(got_seq_no))
        if random() < probability:  # Random packet drop simulation
            decoded = crc.decode([frame], verbose=True)
            if decoded is not None:  # Checksum is correct
                if got_seq_no == exp_seq_no:  # Send ack
                    to_send = rdt_send(got_seq_no)
                    if to_send:
                        s.sendto(to_send, client_address)
                        f_write(decoded, output_file)
                        exp_seq_no = exp_seq_no + 1
                elif got_seq_no > exp_seq_no:  # Future packet received, hence dropped
                    print("Packet loss, sequence number = " + str(got_seq_no))
                elif got_seq_no < exp_seq_no:  # Repeat sent
                    to_send = rdt_send(exp_seq_no)
                    if to_send:
                        # print ("Retransmitted ACK - " + str(got_seq_no))
                        s.sendto(to_send, client_address)
            else: