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 send_window_size(server_socket, window_size, client_addr): pkt = packet(data=str(window_size)) server_socket.sendto(pkt.pack(), client_addr) ack, add = server_socket.recvfrom(600) ack_p = packet(pkd_data=ack, type='ack') if ack_p.checksum == calc_checksum(str(window_size)): print('Received file window size ack...')
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_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 send_file_len(socket, address, data): print('Required file: ' + str(data)) req_file = str(data) pkts = get_packets_from_file(req_file) file_len = len(pkts) pkt = packet(seqno=0, data=((str(file_len)).encode()), type='bytes').pack_bytes() socket.sendto(pkt, address) while True: try: ack, add = socket.recvfrom(600) ack_p = packet(pkd_data=ack, type='ack') if ack_p.checksum == calc_checksum(str(file_len)): print('Received file length ack...') break except socket.timeout: continue
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.bind((host, port)) # Bind to the port #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)