Sticky packet unpacking is a problem at the bottom of the network, which may occur at the data link layer, network layer and transmission layer. Most of our daily network application development is carried out at the transport layer. Because UDP has message protection boundary, sticky packet unpacking problem does not occur, so sticky packet unpacking problem only occurs in TCP protocol.

What is sticky package, unpack?

Suppose that the client sends two consecutive packets to the server, represented by packet1 and packet2, then the data received by the server can be divided into three types, which are listed as follows:

** The first case: ** The receiver normally receives two packets, that is, there is no unpacking and sticky packet phenomenon, which is not within the scope of this article.

** The second case: ** The receiving end receives only one packet. Because TCP does not lose packets, this packet contains the information of two packets sent by the sender. This phenomenon is called sticky packets. This situation is difficult to handle because the receiver does not know the boundary between the two packets.

** Third case: ** This case has two manifestations, as shown in the figure below. The receiver receives two packets, but the two packets are either incomplete or there is an extra one, which is called unpacking and sticky packets. These two cases, without special treatment, are equally difficult to deal with on the receiving end.

Why does TCP packet sticking and unpacking occur?

TCP packet sticking or unpacking occurs due to the following reasons:

  • Unpacking occurs when the application writes data larger than the socket buffer size.

  • Applications write data that is smaller than the socket buffer size. The network adapter sends data that has been written more than once to the network. Sticky packets occur.

  • TCP fragmentation is performed for MSS (Maximum packet Length). When the TCP packet length -TCP header length is greater than MSS, packet disassembly occurs.

  • The receiving method does not read socket buffer data in a timely manner, and sticky packets occur.

Sticking package, unpacking solutions

TCP itself is stream-oriented, as a network server, how to split or merge meaningful information from this continuous flow of data? There are some common methods:

  • 1. The sender adds a header to each packet, which should contain at least the length of the packet. In this way, after receiving data, the receiver can know the actual length of each packet by reading the length field in the header of the packet.

  • 2. The sender encapsulates each packet into a fixed length (if not enough, it can be filled by adding 0), so that the receiver automatically splits each packet by reading the data of a fixed length from the receiving buffer each time.

  • 3. Boundaries can be set between packets, such as adding special symbols, so that the receiver can separate different packets through this boundary.

Refer to the address

  • www.cnblogs.com/panchanggui…

If you like my article, you can follow the individual subscription number. Welcome to leave messages and communicate at any time. If you want to join the wechat group to discuss with us, please add the administrator to simplify the stack culture – little Assistant (lastpass4U), he will pull you into the group.