Esempio n. 1
0
 def connect(self, address: Tuple[str, int]) -> None:
     self.setblocking(False)
     self.target_address = address
     header_1: rdt_header = rdt_header(SYN_bit, self.seq_num, self.ack_num)
     header_2: rdt_header
     while True:
         self.sendto(header_1.to_bytes())
         print("send syn finish")
         time.sleep(1)
         data: bytes = begin_bytes
         try:
             data, addr_1 = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             time.sleep(1)
             continue
         header_2 = rdt_header.unpack(data)
         if check_sum(data) == 0 and header_2.equal(
                 bits=SYN_bit + ACK_bit, ack_num=self.seq_num + 1):
             print("recieve syn ack finish")
             break
         else:
             time.sleep(1)
             print("header 2 is not ok")
     # done in here
     print("jump out")
     self.seq_num += 1
     self.ack_num = header_2.seq_num + 1
     header_3: rdt_header = rdt_header(ACK_bit, self.seq_num, self.ack_num)
     for i in range(3):
         self.sendto(header_3.to_bytes())
     # 这个包丢了实际上没问题,server可以通过正文来判断
     print(time.time() - self.begin_time, "finish first stege")
     print(self.seq_num, self.ack_num,
           "this is seq and seq ack of connect finish")
Esempio n. 2
0
 def close_server(self) -> None:
     self.clear_buffer()
     print("server begin close")
     header_1: rdt_header
     header_2: rdt_header
     while True:
         time.sleep(0.01)
         print("run in three")
         try:
             data, _uesless_ = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             pass
         try:
             header_1: rdt_header = rdt_header.unpack(data)
         except UnboundLocalError:
             print(3)
             continue
         if header_1.equal(bits=FIN_bit) and check_sum(data) == 0:
             self.sendto(
                 rdt_header(ACK_bit, self.seq_num,
                            header_1.seq_num + 1).to_bytes())
             self.sendto(
                 rdt_header(FIN_bit + ACK_bit, self.seq_num,
                            header_1.seq_num + 1).to_bytes())
             break
         else:
             print(header_1)
     while True:
         time.sleep(1)
         print("run in four")
         self.sendto(
             rdt_header(ACK_bit, self.seq_num,
                        header_1.seq_num + 1).to_bytes())
         self.sendto(
             rdt_header(FIN_bit, self.seq_num,
                        header_1.seq_num + 1).to_bytes())
         try:
             data_4, _useless_ = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             print(e)
             traceback.print_exc()
         try:
             header_4: rdt_header = rdt_header.unpack(data_4)
         except (UnboundLocalError, struct.error) as e:
             print(e)
         if check_sum(data_4) == 0 and header_4.equal(
                 bits=ACK_bit, ack_num=self.seq_num + 1):
             break
     time.sleep(1)
     self.seq_num = 0
     self.ack_num = 0
     print("Connect Close")
Esempio n. 3
0
 def close_client(self) -> None:
     self.setblocking(False)
     self.clear_buffer()
     self.send("")
     header_1: rdt_header
     header_2: rdt_header
     while True:
         time.sleep(0.1)
         print("run in one")
         header_1 = rdt_header(FIN_bit, self.seq_num, self.ack_num)
         self.sendto(header_1.to_bytes())
         try:
             data, addr = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             print(e)
             # traceback.print_exc()
             continue
         try:
             header_2 = rdt_header.unpack(data)
         except (UnboundLocalError, struct.error) as e:
             print(e)
             traceback.print_exc()
             continue
         if check_sum(data) == 0 and header_2.equal(bits=ACK_bit, ack_num=self.seq_num + 1) or \
                 header_2.equal(bits=FIN_bit + ACK_bit, ack_num=self.seq_num + 1):
             break
     while True:
         print("client close stage 3")
         self.sendto(
             rdt_header(ACK_bit, self.seq_num + 1,
                        header_2.seq_num + 1).to_bytes())
         self.clear_buffer()
         print("client close stage buffer clear")
         time.sleep(2)
         try:
             data_3, _useless_ = self.recvfrom(data_length, )
         except (BlockingIOError, ConnectionResetError):
             break
     print("{} {}".format(self.seq_num, self.ack_num))
     time.sleep(1)
     print("Connect Close")
     super().close()
Esempio n. 4
0
 def accept(self) -> Tuple['rdt_socket', Tuple[str, int]]:
     """
     相当于这玩意的作用是
     socket本身接受一个socket,然后new一个socket with recieve 地址返回.
     """
     # receive syn ; send syn, ack; receive ack
     self.client = False
     self.setblocking(True)
     header_1: rdt_header
     header_2: rdt_header
     header_3: rdt_header
     while True:
         self.setblocking(True)
         try:
             data: bytes
             addr: Tuple[str, int]
             data, self.target_address = self.recvfrom(data_length, )
         except TypeError as e:
             print("line 112")
             continue
         print("recieve syn data")
         self.setblocking(False)
         header_1 = rdt_header.unpack(data)
         if check_sum(data) == 0:
             if header_1.equal(bits=SYN_bit):
                 self.ack_num = header_1.seq_num + 1
                 header_2: rdt_header = rdt_header(bits=SYN_bit + ACK_bit,
                                                   seq_num=self.seq_num,
                                                   ack_num=self.ack_num)
                 self.sendto(header_2.to_bytes())
                 print("send syn,ack finish")
                 break
         else:
             print("line 120")
     while True:
         time.sleep(1)
         data: bytes = begin_bytes
         try:
             data, addr_2 = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             time.sleep(1)
         header_3: rdt_header = rdt_header.unpack(data)
         print(header_3, check_sum(data), "can it jump out?")
         if check_sum(data) == 0 and header_3.equal(
                 bits=ACK_bit, ack_num=self.seq_num + 1):
             break
         self.sendto(header_2.to_bytes())
     print(header_3.bits, header_3.ack_num, self.seq_num + 1)
     self.seq_num += 1
     print("this is addr_2 ", addr_2)
     print(time.time() - self.begin_time)
     print(self.seq_num, self.ack_num,
           "this is seq and seq ack of accept finish")
     return self, self.getsockname()
Esempio n. 5
0
 def recvall(self) -> str:
     data_willreturn: str = ""
     count: int = 0
     while True:
         print("count is {}".format(str(count)))
         print("length of receive is {}".format(len(data_willreturn)))
         time.sleep(0.01)
         try:
             data, addr = self.recvfrom(data_length, )
         except (BlockingIOError, TypeError) as e:
             traceback.print_exc()
             continue
         if len(data) == 0:
             continue
         try:
             body: rdt_body = rdt_body.unpack(data)
         except (BlockingIOError, struct.error) as e:
             traceback.print_exc()
             continue
         print("{} is body.seq_num".format(str(body.seq_num)))
         if check_sum(data) == 0 and len(data) > header_length and body.equal(bits=ACK_bit) \
                 and body.seq_num // udp_packet_length == count:
             count += 1
             self.ack_num += body.length
             self.sendto(
                 rdt_header(ACK_bit, self.seq_num, body.seq_num).to_bytes())
             try:
                 data_willreturn += body.data_bytes.decode(encoding='utf-8')
             except UnicodeEncodeError:
                 pass
         else:
             self.sendto(
                 rdt_header(ACK_bit, self.seq_num, self.ack_num).to_bytes())
             print("Wrong packet")
         if check_sum(data) == 0 and body.equal(bits=ACK_bit) is False:
             break
     return data_willreturn