Example #1
0
 def close2(self, data):
     res = parse_link_header(data)
     ack = res[1]
     if ack == self.seq + 1:
         print('close....')
         self.status = 'CLOSED'
         return True
Example #2
0
    def send_file(self):
        with open(self.filename, 'rb') as f:
            data = True
            # self.seq = 0
            while data:
                # 接收确认并调整缓存等
                try:
                    data, addr = self.sock.recvfrom(HEAD_LEN)
                except:
                    pass
                else:
                    if check_data(HOST, local_ip, data):
                        res = parse_link_header(data)
                        ack = res[1]
                        self.deal_buf(ack, self.buf)

                if len(self.buf[0]) >= self.wn_size:
                    self.send_timeout_data()
                    time.sleep(2)
                    continue

                data = f.read(self.data_length)
                time_data = make_timestamp()
                self.buf[2].append(time_data[0])
                self.buf[0].append(self.seq)
                self.buf[1].append(data)
                data_head = make_link_header(self.seq,
                                             pack_len=self.data_length,
                                             flag=2,
                                             time_stamp=time_data[1])
                data = data_head + data
                data = fill_checknum(local_ip, HOST, data)
                self.sock.sendto(data, self.server)
                self.seq += self.data_length
Example #3
0
 def make_handshake(self, data):
     self.seq = random.randint(0, 2**16)  # y
     self.ack = parse_link_header(data)[0] + 1  # x +1
     flag = 3
     return make_link_header(self.seq,
                             self.ack,
                             flag=flag,
                             time_stamp=data[-8:-4])
Example #4
0
 def deal_fin2(self, data):
     res = parse_link_header(data)
     seq, ack = res[0], res[1]
     if ack == self.ack and data[11] == 6:
         return make_link_header(ack,
                                 seq + 1,
                                 flag=2,
                                 time_stamp=data[-8:-4])
Example #5
0
    def close1(self, data, addr):
        res = parse_link_header(data)
        self.seq += 1
        self.seq %= MAX_SEQ
        ack = res[0] + 1
        flag = 2
        data = make_link_header(self.seq,
                                ack,
                                flag=flag,
                                time_stamp=data[-8:-4])
        data = fill_checknum(HOST, addr[0], data)
        self.sock.sendto(data, addr)
        self.status = 'CLOSE-WAIT'

        self.seq %= MAX_SEQ
        data = make_link_header(self.seq, ack, flag=6)
        data = fill_checknum(HOST, addr[0], data)
        self.sock.sendto(data, addr)
        self.status = 'LAST-ACK'
Example #6
0
 def parse_third_data(self, data):
     res = parse_link_header(data)
     seq, ack = res[0], res[1]
     if seq == self.ack and ack == self.seq + 1:
         return True
Example #7
0
 def deal_fin1(self, data):
     res = parse_link_header(data)
     self.ack = res[1]
     if self.ack == self.seq + 1 and data[11] == 1:
         return True
Example #8
0
 def parse_ack_data(self, data):
     seq, ack, pack_len, ts = parse_link_header(data)
     seq += 1
     if ack == self.seq + 1 and data[11] == 3:
         flag = 2
         return make_link_header(ack, seq, flag=2)