def recv_window_size(self,socket): pkt, adr = socket.recvfrom(600) unpkd = packet(pkd_data=pkt, type='bytes') ack_p = ack(checksum=calc_checksum(unpkd.data.decode()), seqno=0).pack() socket.send(ack_p) self.window_size = int(unpkd.data) print('Window size = ', self.window_size)
def recv_file_len(self, socket): pkt, adr = socket.recvfrom(600) unpkd = packet(pkd_data=pkt,type='bytes') ack_p = ack(checksum=calc_checksum(unpkd.data.decode()),seqno=0).pack() socket.send(ack_p) self.file_len = int(unpkd.data) print('Required file length = ', self.file_len, ' packets.')
def recv_stop_and_wait(self): client.recv_file_len(self.my_socket) seqno = 0 pkt_num = 0 corrupted = self.get_corrupted_packets(self.file_len,0.05,5) filename = './Clients/'+str(self.server_port)+'/dl_saw_' + str(self.requested_filename) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) file = open(filename,'wb') while True: received_pack , addr = client.my_socket.recvfrom(600) received_packet = packet(pkd_data=received_pack,type='bytes') if received_packet.seqno in corrupted: received_packet.checksum = received_packet.checksum - 10 corrupted.remove(received_packet.seqno) if(received_packet.checksum==structures.calc_checksum(received_packet.data,type='bytes') and received_packet.seqno==seqno): print(received_packet.data) file.write(received_packet.data) ack_packet = structures.ack(seqno= seqno,checksum=received_packet.checksum) client.my_socket.send(ack_packet.pack()) seqno=(seqno+1)%2 pkt_num += 1 if pkt_num == self.file_len: print('File received.') file.close() break elif(received_packet.seqno!= seqno): client.my_socket.send(ack_packet.pack())
def recv_go_back_n(self): client.recv_file_len(self.my_socket) print('Connected to socket #' + str(self.my_socket.getsockname()[1])) corrupted = self.get_corrupted_packets(self.file_len,0.05,5) exp_pkt_num = 0 while True: try: pkt, adr = self.my_socket.recvfrom(600) recv_pkt = packet(pkd_data=pkt, type='bytes') if recv_pkt.seqno in corrupted: recv_pkt.checksum = recv_pkt.checksum-10 corrupted.remove(recv_pkt.seqno) if adr[0] == self.server_ip: print('Received packet# '+str(recv_pkt.seqno)) # receive packets initally. cs = recv_pkt.checksum ack_pkt= ack(seqno=recv_pkt.seqno, checksum=cs) pkd_ack = ack_pkt.pack() if recv_pkt.seqno == exp_pkt_num and recv_pkt.checksum == calc_checksum(recv_pkt.data, type='bytes'): print('Sending Ack# ' + str(recv_pkt.seqno)) self.recv_pkt_list.append(recv_pkt) # if packets not corrupted and came in order, add to list. self.my_socket.send(pkd_ack) # and send ack exp_pkt_num += 1 else: # else discard packet, will be received again. if recv_pkt.checksum != calc_checksum(recv_pkt.data,type='bytes'): print('Packet # ', recv_pkt.seqno,'is corrupted, re-receiving') continue if self.file_len == len(self.recv_pkt_list): # if all file received, break. print('File received successfully.') break except socket.timeout: print('Packet# ', exp_pkt_num, ' timed out, re-receiving.') continue self.write_file(self.recv_pkt_list)
def recv_selective_repeat(self): client.recv_file_len(self.my_socket) self.recv_window_size(self.my_socket) window_size = self.window_size #window_size = params[0] packet_number = 0; buffer = "" # divides file content into chunks of packet size next_seqno = 0 send = True file_content = "" pkt_num=0 window = [] window_seqno = [] recv_base = 0 corrupted = self.get_corrupted_packets(self.file_len,0.05,5) filename = './Clients/'+str(self.server_port)+'/dl_sr_' + str(self.requested_filename) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) file = open(filename,'wb') while True: while (len(window) < window_size): # fills window received_pack, addr = self.my_socket.recvfrom(600) received_packet = packet(pkd_data=received_pack,type='bytes') if received_packet.seqno in corrupted: received_packet.checksum = received_packet.checksum - 10 corrupted.remove(received_packet.seqno) if (received_packet.checksum == structures.calc_checksum( received_packet.data,type='bytes')): ack_packet = structures.ack(seqno=received_packet.seqno, checksum=received_packet.checksum) client.my_socket.send(ack_packet.pack()) if(not (received_packet.seqno in window_seqno)): #print(received_packet.data) window.append(received_packet) window_seqno.append(received_packet.seqno) window = arrange_window(window, recv_base) while (len(window) > 0 and recv_base == window[0].seqno): data =window.pop(0).data print(data) file.write(data) pkt_num += 1 window_seqno.remove(recv_base) recv_base = (recv_base + 1) % window_size if pkt_num == self.file_len: print('File received.') file.close() exit(0)
def receive_ack(socket, lock, received_acks): "used for selective repeat" lock.acquire(blocking=True) try: packed_data, addr = socket.recvfrom(600) except Exception: lock.release() return a = ack(pkd_data=packed_data) received_acks.append(a) lock.release() return
def stop_and_wait(server_socket, filename, client_addr): send_file_len(server_socket, client_addr, filename) print('Sending using stop and wait.') file_content = readfile(filename) packet_number = 0 seqno = 0 send = True lost_list = lost_packets( len(file_content) // packet_size, probability, random_seed) while (packet_number < len(file_content) / packet_size): if (len(lost_list) > 0 and packet_number == int(lost_list[0])): lost_list.pop(0) send = False if (send): send = False start_index = packet_number * packet_size end_index = packet_number * packet_size + packet_size if (end_index < len(file_content)): buffer = file_content[start_index:end_index] else: buffer = file_content[start_index:] send_packet = structures.packet(seqno=seqno, data=buffer, type='bytes') packed_packet = send_packet.pack_bytes() server_socket.sendto(packed_packet, client_addr) try: ack_pack, addr = server_socket.recvfrom(600) except timeout: send = True #resend continue ack__packet = structures.ack(pkd_data=ack_pack) if (ack__packet.checksum == send_packet.checksum and ack__packet.seqno == seqno): packet_number = packet_number + 1 seqno = (seqno + 1) % 2 send = True
def go_back_n(file_name, server_socket, client_address, window_size=5): print('Sending using go back n.') pkt_list = get_packets_from_file(file_name) # converts file into packets. send_file_len(server_socket, client_address, file_name) # sends required file length to client(packets) flag = False i = 0 #server_socket.settimeout(5) lost_pkts = lost_packets(len(pkt_list), probability, random_seed) # lost packets seqno while i < len(pkt_list): current_pkt = pkt_list[i:window_size + i] # current packets in window if window_size + i > len(pkt_list): current_pkt = pkt_list[i:] for pkt in current_pkt: # send all packets in window send = True if pkt.seqno in lost_pkts: # if packet to is lost, don't send. send = False lost_pkts.remove(pkt.seqno) if send: print('Sending packet # ', pkt.seqno) pkd_packet = pkt.pack_bytes() server_socket.sendto(pkd_packet, client_address) if flag is True and not send: flag = False break for pkt in current_pkt: # receive acks for sent pacekts in window try: ack_pkt = server_socket.recv(600) unpkd_ack = ack(pkd_data=ack_pkt) if unpkd_ack.checksum == pkt.checksum: # if ack received and not corrupted, increment current_pkt i += 1 print('Ack# ' + str(unpkd_ack.seqno) + ' received') else: print('Ack# ', unpkd_ack.seqno, ' is corrupted..') # else, go back n. flag = True break except socket.timeout as e: # if an ack times out, go back n. send = True print('Ack # ' + str(pkt.seqno) + ' ack has timed out....resending packet') break
def send_request(self): while True: request_packet = packet(seqno=0, data=self.requested_filename) request_pack = request_packet.pack() self.my_socket.send(request_pack) self.my_socket.settimeout(5) try: rcv, adr = self.my_socket.recvfrom(1024) ack_pkt = ack(pkd_data=rcv) if ack_pkt.checksum == calc_checksum(request_packet.data): print('Request sent..') break else: continue except socket.timeout: print('Request ack timed out.., resending') continue print('File: ' + str(self.requested_filename) + ' has been requested from the server.') self.my_socket.settimeout(None)
#s.setblocking(1) clients = [] # list of online clients serving_port = 49151 # port used to send the file used_ports = [] # list of ports used to send the file req_err = True while True: print('Waiting for connection... ') request_data, addr = s.recvfrom(1024) # receives packet from clients pkt = packet(pkd_data=request_data) if req_err: req_err = False pkt.checksum = pkt.checksum - 10 if pkt.checksum == calc_checksum(pkt.data): ack_pkt = ack(seqno=0, checksum=calc_checksum(pkt.data)) s.sendto(ack_pkt.pack(), addr) print('File request received, sending ack..') else: print('File request Corrupted..') continue if (addr not in clients): # keep track of clients clients.append(addr) while serving_port in used_ports: # searching for a free port serving_port = serving_port - 1 if (serving_port < 1024): serving_port = 49151 used_ports.append(serving_port) s.sendto(packet(data=str(serving_port), seqno=0).pack(), addr)