In our applications involving the TCP protocol, the problem of sticky packets often occurs. The so-called sticky packet, simply put, means that I have two messages, obviously the code of the sender is sent in two times, but the two messages are received at one time at the receiver. This situation is very common in both the embedded industry and the Internet industry.
- Why does the TCP protocol stick to packets?
Then you need to understand the definition of TCP first. TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol.
Among them, the biggest relationship with sticky packets is based on the feature of byte stream. The byte stream can be understood as the data flowing in a two-way channel. This data is actually the binary data we often say, which is simply a lot of 01 strings. There are no boundaries between these 01 strings.
The data sent by the application layer to the TCP protocol is not sent to the destination host in message units, but sent to the downstream in the form of byte streams. These data may be cut and assembled into various data packets, and the receiving end receives these The previous message is not restored correctly after the data package, so the sticky package phenomenon occurs.
Then why can't it be restored correctly? There are two main reasons:
- The reason for the sender
When the sender assembles the message, it combines several small packets into one packet, so that the receiver cannot parse out the small packets. This corresponds to Nagle's algorithm. Because both TCP and Nagle's algorithm are products of the last century, doing so in early networks can significantly reduce network stress. Otherwise, sending small packets with only a few bytes frequently will seriously waste network IO performance.
However, in the modern Internet, the network performance has been greatly improved. It seems that the IO performance improved by the Nagle algorithm is not so important. On the contrary, due to the operation of waiting for the data to be merged, the transmission delay will become larger. In the application of online games , it will greatly affect the experience. So it is generally turned off now.
- Reasons on the receiving end
After the receiving end receives the message, the application layer cannot always take the data immediately, and there is always a receive buffer. If the interval between two independent messages entering the buffer is too small, the application layer cannot take the previous message in the middle of the two messages, then the next time it is read, the two packets of messages are bound to be read at the same time, which will also lead to sticky packets.
And this situation cannot be avoided by letting the sender send packets evenly in time, because of the existence of network instability, even packets sent evenly in time may appear randomly to the receiver.
Second, how to avoid the negative impact of sticky bags?
According to the above analysis, it is not difficult to find that it is basically impossible to eliminate the problem of sticky bags. Even if the sender and receiver can control themselves, the process of network transmission is difficult to control.
But even if the problem of sticky packets exists, it does not affect our large-scale use of the TCP protocol. Because this problem is very well handled at the application layer. There are roughly two ideas:
- Add a special flag as a separator in the message
In this way, when the application layer detects the special separator, it knows that this is the start and end of a packet, and can perform operations such as fragmentation, and the problem is solved.
However, there are some disadvantages. For example, if the delimiter is defined as "12345678", what if the string of "12345678" appears in the message content? This will cause the message to be sliced abnormally, resulting in the wrong message being received. However, if you can control the content of the message and ensure that the content of "12345678" will not appear in it, this method is more flexible.
- Add the length of the message
Read the message length information according to the agreed length field, and then read the message content according to the message length information. This is also a very common method and is reflected in many protocols.
- Add package header
The sender adds a packet header to each data packet, and the header should contain at least the length of the data packet, so that the receiver can know the actual length of each data packet by reading the length field of the packet header after receiving the data.
The above is the content of this issue about solving the problem of TCP sticky packets. Small code words are not easy, please like and support! See you next time~~
Top comments (0)