Analysis of the causes and situations of TCP sticky packets and unpacking and solutions

Table of contents

Causes of TCP sticky packets

Analysis of TCP sticky packet unpacking

Solutions for sticking and unpacking 


Causes of TCP sticky packets

TCP is a streaming protocol, which is transmitted in the form of data stream during the transmission process. The so-called stream is a string of data without boundaries. Just like a river, it is formed into one piece, and there is no complete boundary between data and data. The bottom layer of TCP does not understand the specific meaning of the upper layer business data. It will divide the packets according to the actual situation of the TCP buffer, so in The business believes that a complete packet may be split into multiple packets by TCP for sending, or multiple small data packets may be encapsulated into one large data, which is the reason for the so-called TCP sticky packets and unpacking

Analysis of TCP sticky packet unpacking

Assuming that the client sends two data packets A1 and A2 to the server respectively, since the number of bytes read by the server at one time is uncertain, the following four situations may exist.

1. The server has received two independent data packets A1 and A2, and there is no sticking or unpacking.

2. The server receives two complete data packets of A1 and A2 at the same time, and A1 and A2 are glued together, which is called TCP sticky packet

 

3. The server reads two data packets twice, the first time it reads part of the data packet of A1, and the second time it reads the remaining data packet of A1 and the complete A2 complete data packet, which is called TCP Unpacking is to split the complete data packet of A1 into two or even multiple data packets for sending

4. The server reads two data packets twice, the first time it reads the complete data packet of A1 and the partial data packet of A2, and the second time it reads the remaining data packet of A2, which is also called TCP Unpacking is to split the complete data packet of A2 into two or even multiple data packets for sending

If the TCP sliding window of the server is very small at this time, and the data packets A1 and A2 are relatively large, the fifth situation is likely to occur, that is, the server can send the A1 and A2 packets completely in multiple times, and multiple unpacking occurs during the period

Solutions for sticking and unpacking 

For sticking and unpacking problems, there are four common solutions:

  1. When the client sends data packets, each packet has a fixed length, such as 1024 bytes in size. If the length of the data sent by the client is less than 1024 bytes, it will be completed to the specified length by adding spaces;
  2. The client uses a fixed delimiter at the end of each packet, such as \r\n. If a packet is split, wait for the next packet to be sent to find the \r\n, and then split it The header part of the previous package is merged with the rest of the previous package, so that a complete package is obtained;
  3. The message is divided into a header and a message body, and the length of the current entire message is stored in the header, and a complete message is read only after a message of sufficient length is read;
  4. Handle sticking and unpacking through custom protocols.

 

Guess you like

Origin blog.csdn.net/asdasd121312dasd/article/details/127974960