Ejemplo n.º 1
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
Ejemplo n.º 2
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])
Ejemplo n.º 3
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])
Ejemplo n.º 4
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'
Ejemplo n.º 5
0
 def send_timeout_data(self):
     t = time.time()
     for index, time_stamp in enumerate(self.buf[2]):
         if t - time_stamp > self.time_out:
             time_data = make_timestamp()
             self.buf[2][i] = time_data[0]
             data_head = make_link_header(self.buf[0][i],
                                          pack_len=self.data_length,
                                          flag=2,
                                          time_stamp=time_data[1])
             data = data_head + self.buf[1][i]
             data = fill_checknum(local_ip, HOST, data)
             self.sock.sendto(data, self.server)
Ejemplo n.º 6
0
    def release(self):
        self.seq += 1
        self.seq %= MAX_SEQ
        data = make_link_header(self.seq, flag=4)
        data = fill_checknum(local_ip, HOST, data)
        self.sock.sendto(data, self.server)
        self.status = 'FIN-WAIT-1'

        data, addr = self.sock.recvfrom(HEAD_LEN)
        if check_data(HOST, local_ip, data):
            if self.deal_fin1(data):
                self.status = 'FIN-WAIT-2'
        #  关闭连接第二阶段
        data, addr = self.sock.recvfrom(HEAD_LEN)
        if check_data(HOST, local_ip, data):
            data = self.deal_fin2(data)
            if data:
                data = fill_checknum(local_ip, HOST, data)
                self.sock.sendto(data, self.server)
                self.status = 'TIME-WAIT'
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
 def make_start_head(self):
     self.seq = random.randint(0, 2**16)  # x
     flag = 1
     return make_link_header(self.seq, flag=flag)