Packet sticking and unpacking of TCP

        When the client keeps sending data packets to the server, two data packets will be connected together, which is the sticking and unpacking often encountered in our TCP protocol;

        The two most important protocols in the transport layer: UDP and TCP, but TCP is much more complicated than UDP; UDP is connectionless, and TCP is connection-oriented; therefore, only TCP will stick and unpack;

       The manifestation of TCP sticking and unpacking:

 the first case

        When sending two data packets, each time the server reads one, the situation is normal;

second case

        Two data packets are connected together, and a sticky packet phenomenon occurs;

third case

       When P1 is too large or P2 is too large, only half of P1 or P2 is received at a time, which is the phenomenon of unpacking;

Fourth case

      When both P1 and P2 are very large, and the sliding window of TCP is still very small, at this time, P1 and P2 will be unpacked many times;

 

Reasons for sticking and unpacking:

1. If the data to be sent is larger than the remaining space of the TCP send buffer, unpacking will occur;

2. If the data to be sent is larger than MSS (maximum message length), TCP will unpack before transmission.

3. The data to be sent is smaller than the size of the TCP send buffer. TCP will send the data written to the buffer multiple times at one time, and a sticky packet will occur.

4. If the application layer of the receiving data end fails to read the data in the receiving buffer in time, sticky packets will occur.

5. The payload of the Ethernet frame is larger than the MTU for IP fragmentation.

etc......

The solution to sticky unpacking:

(1) The message has a fixed length, for example, the size of each message is a fixed length of 200 bytes. If it is not enough, fill the blanks with blanks;

(2) Add a carriage return and line feed at the end of the packet for segmentation, such as FTP protocol;

(3) Divide the message into a message header and a message body. The message header contains a field representing the total length of the message (or the length of the message body). Usually, the design idea is that the first field of the message header uses int32 to represent the total length of the message;

(4) The ring buffer solves the sticky packet problem

(5) More complex application layer protocols.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326286908&siteId=291194637
Recommended