In Socket network programming, all communication is end-to-end. The quintuple composed of client port + server port + client IP+ server IP+ transmission protocol can clearly identify a connection. In TCP socket programming, both the sender and receiver have pairs of sockets. In order to send multiple packets to the receiving end more efficiently, the sending end adopts the optimization algorithm (Nagle algorithm), which combines the data with small interval and small data volume into a data block with large data volume, and then carries out packet encapsulation. In this case, the receiver must use an efficient and scientific unpacking mechanism to distinguish the data.

1.Q: What is the TCP Sticky Packet Problem? TCP sticky packet refers to several packets of data sent by the sender are stuck into a packet when they reach the receiver. From the perspective of the receiving buffer, the head of the last packet of data is immediately followed by the tail of the previous packet of data. There are many reasons for sticky packet, which may come from the sender or the receiver.

2.Q: Causes of TCP packet stickiness (1) The sender

TCP default uses Nagle algorithm (main function: reduce the number of packet segments in the network), and Nagle algorithm mainly does two things:

Only when the previous packet is confirmed will the next packet be sent to collect multiple small packets. The Nagle algorithm that sends one packet together when an acknowledgement arrives may cause the sender to have a sticky packet problem

(2) Recipient reasons

When TCP receives a packet, it is not immediately sent to the application layer for processing, or the application layer does not process it immediately. In fact, TCP stores the received packets in the receive cache, and the application actively reads the received packets from the cache. This way, if TCP can receive packets into the cache faster than the application can read them from the cache, multiple packets will be cached and the application may read multiple packets that are stuck together end to end.

3.Q: When do you need to deal with the sticky balling? If the sender to send more groups of different parts of the data is the same piece of data, such as a file is divided into several parts to send, then, of course, don’t need to deal with glue phenomenon If multiple groups have nothing to do, even the parallel relationship, then this time must be the phenomenon of packet processing viscosity 4. Q: how to deal with sticky package phenomenon? (1) The sender

Sticky packet problems caused by the sender can be resolved by turning off the Nagle algorithm, using the TCP_NODELAY option.

(2) Receiving Party

The receiver has no way to deal with the sticky packet and can only pass the problem to the application layer.

(2) Application layer

The solution of the application layer is simple and feasible, which can not only solve the sticky packet problem of the receiver, but also solve the sticky packet problem of the sender.

Solution: Loop processing. When an application reads a packet from the receive cache, it should read the next piece of data through a loop until all the data has been processed. But how to determine the length of each piece of data?

Formatting data: Each piece of data has a fixed format (start, end). This is easy to do, but when selecting start and end characters, make sure that each piece of data does not contain the start and end characters inside. Sending length: when sending each piece of data, the length of the data is sent together. For example, the first four bits of the data are the length of the data. The application layer can judge the start and end positions of each group according to the length during processing. 5.Q: Does UDP cause packet sticky problems? TCP to ensure reliable transmission and reduce additional costs (each contract to verify), adopted based on the transmission flow, based on the flow of transmission don’t think the news is a a, is no protection message boundaries (protection message boundaries: refers to the transport protocol data as a separate message transmission on the Internet, the receiver can only accept a separate message at a time).

UDP, on the other hand, is message transmission oriented and protected message boundaries. The receiver only receives one independent message at a time, so there is no sticky packet problem.

Here’s an example: There are three packets with the size of 2K, 4K and 6K respectively. If UDP is used to send the packets, no matter how big the receiving cache of the recipient is, we have to send at least three times before sending the packets. However, if TCP is used to send the packets, we only need the receiving cache of the recipient to be 12K in size. You can send all these 3 packets at one time. ———————————————— Copyright Notice: This article is the original work of the CSDN blogger “Yuxi King”. It is under the CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this notice to reprint. The original link: https://blog.csdn.net/weixin_…