Tommy

Tommy

写代码是热爱,但也是生活 !
github

What is packet sticking? Why does it occur? How to solve it?

Preface#

What is packet sticking? Why does it occur? How can it be resolved?

1. What is packet sticking?#

  1. Packet sticking occurs between the transport layer and the application layer. Although the transmission between them is in chunks, TCP considers these chunks of data as unstructured byte streams without boundaries.
  2. From the frame structure of TCP, it can be seen that there is no field indicating the length of the data in the header.

From the above, it can be seen that packet sticking is when one packet contains the information of two packets. The receiving end receives two packets, but either they are incomplete or there is an extra chunk, resulting in the occurrence of packet splitting and sticking. The issue of packet splitting and sticking makes it difficult for the receiving end to process because it cannot distinguish a complete packet.

2. Why does packet sticking occur?#

Packet sticking phenomenon occurs in the transport layer TCP.

  1. Packet sticking occurs at the sending end.
    When the transmission between the client and the server adopts TCP protocol to maintain a long connection state (of course, the first connection and request will not experience packet sticking), but when the sent packets are too small, TCP protocol will enable the Nagle algorithm by default to merge these smaller packets for sending (the sending of buffered data is a pile-up process); this merging process occurs in the sending buffer, which means that the data has already become a stuck packet once it is sent out.
  2. Packet sticking occurs at the receiving end.
    The process of receiving data using TCP protocol at the receiving end is as follows: the data arrives at the receiving end and is passed from the lower layer of the network model to the transport layer. The TCP protocol in the transport layer places it in the receiving buffer, and then the application layer actively retrieves it. At this time, a problem arises, which is that the read data function called in the program cannot promptly retrieve the data from the buffer, and the next data arrives and some of it is placed at the end of the buffer. When we read the data, it becomes a stuck packet. (The speed of putting data > the speed of the application layer retrieving data)

3. How to solve packet splitting and sticking#

There are generally two common methods for packet splitting mechanism:

  • Special character control
  • Adding the length of the data packet to the header.

If using Netty, there are dedicated encoders and decoders to solve the issue of packet splitting and sticking.

Note: UDP does not have the issue of packet sticking, but it has packet loss and disorder. There are no incomplete packets received, and all received packets are completely correct. The protocol for transmitting data units is UDP datagrams or user datagrams, and they are neither merged nor split during transmission.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.