TCP packet problem

Unpacking and grouping

The TCP protocol can split the data to be transmitted into several data packets, and send them to the remote host under the premise of ensuring the transmission order of the data packets, and assemble them back to the original.

Examples are as follows:

 

 When the packet 1.0.0 is very large, you will find that the transmission of the 2.0.0 data packet is delayed. If the transmission of the data packet 1.0.0 fails, the entire request will be sent.

In order to avoid the above situation, we will split the 1.0.0 packets into smaller packets and put them into the sending queue, and assemble and restore these packets when they are received. The effect is as follows.

 

 As shown in the figure, we split the original 1.0.0 into 1.1.0 + 1.2.0 + 1.3.0 + 1.4.0, so that other data packets have the opportunity to be inserted into it for transmission without waiting for the entire The requested data transfer is complete. This improvement is applicable to scenarios where transmission is restricted and fairness needs to be considered. In addition, if a request for a certain data packet fails during transmission, you can continue to transmit from the failed data packet, which is suitable for scenarios where resuming transmission is required.

Breaking and sticking

The TCP protocol will divide or merge the data to be transmitted according to the size of the packet (Nagle algorithm). Therefore, the same TCP data packet may contain the contents of multiple self-defined data packets. Small data packets may be sent at one time, and large data packets are divided and sent.

 

As shown in the figure: the buffer is a complete data packet 1 and only half of the application data packet 2. The TCP connection is transmitting application data packets 2, 3, 4, and 5, which are divided into 3 TCP data packets, divided into 3 Times.

The first time the application reads data from the buffer is only packet 1 and half of packet 2. Regardless of the split situation, packet 2 is broken, which is called a broken packet.

When reading from the buffer for the second time, the second half of the application data packet 2, part of the application data packets 3, 4 and part of the application data packet 5 will be read. The application data packets 3 and 4 are complete, but it seems that the data packet 4 is stuck behind the data packet 3 and is called a sticky packet.

When an application reads data from a socket, it actually reads the data from the operating system buffer to the application buffer. Therefore, even if we use the TCP NODELAY algorithm to prohibit TCP from applying the Nagle algorithm to merge and send data packets, there is no guarantee that the receiver will not stick to the packet.

Packet technology can solve the above problem: add data header and data tail mark to the data to clarify the beginning and end of each data packet.

Packet structure

  • Packet ID: ID of the packet to which it belongs.
  • Serial number: The serial number of the subcontract.
  • Packet size: The data length of this packet.
  • Data segment: actual data content.

Guess you like

Origin www.cnblogs.com/wangb0402/p/12674563.html
Recommended