SoFunction
Updated on 2025-03-08

Summary of the reasons for packet loss based on TCP communication (recommended)

The company's project is the bottom layer of TCP. Because it is reliable, it is automatically disconnected and reconnected, it is implemented at the bottom layer. However, I remember that TCP will also have packet drop problems, so this article was born - Regarding the problem of TCP packet drops, TCP implements reliable transmission based on an unreliable network, and there will definitely be packet drops.

If data is missing or packet loss is found in communication, the biggest possibility is that there are problems with the process of sending or receiving the process of the program.

For example, if the server sends a large amount of data to the client and the frequency of Send is very high, then an error may occur during Send (there may be many reasons, which may be program processing logic problems, multi-threaded synchronization problems, buffer overflow problems, etc.). If the Send fails to resend data, the client will receive less data than the theory should receive, which will cause data loss and packet loss.

In fact, this phenomenon is not essentially a packet loss or data loss. It is just that there is an error in the program processing, some data is not successfully sent out by the socket.

Commonly used solutions are as follows: unpacking, adding package headers, sending, and combining packages. If the client and server are disconnected, heartbeat tests are often used.

tcp is a "stream" protocol. A complete packet may be split by TCP into multiple packets for transmission, or it may encapsulate small packets into large packets to send. This is the so-called TCP sticking and unpacking problem.

Description of the problem of sticking and unpacking

Assuming that the client sends data packets D1 and D2 to the server, since the number of bytes read by the server at one time is uncertain, there may be four situations below.

1. The server reads two independent packets in two times, namely D1 and D2, without sticking and unpacking;

2. The server receives two packets at once, and D1 and D2 are glued together, becoming TCP stickers;

3. The server reads two data packets in two times, and the first time it reads part of the complete D1 and D2 packets, and the second time it reads the remaining content of the D2 packet, which is called unpacking;

4. The server reads two data packets in two times, the first time it reads part D1, and the second time it reads the remaining part and complete D2 packets of D1;

If the server TCP receiving sliding window is very small at this time, and the data packets D1 and D2 are both large, it is very likely that the fifth possibility will be sent, that is, the server can receive D1 and D2 completely, and unpacking will occur many times during this period. (TCP receiving sliding window: It is the size of the receiver, which changes with the traffic size. If my explanation is not clear, please Baidu on your own, or check the content of TCP in "Computer Network" and "TCP/IP")

Solving strategies for sticking problems

Since the underlying TCP cannot understand the business logic of the upper layer, it is impossible to ensure that the packets are not split and reorganized at the bottom layer. This problem can only be solved through the design of the upper layer application protocol stack. According to the solutions of mainstream protocols in the industry, it is summarized as follows:

1. The message length is fixed, for example, the size of each message is 200 bytes in a fixed length. If it is not enough, fill in the space;

2. Add carriage return line breaks at the end of the package for segmentation, such as the FTP protocol;

3. Divide the message into a message header and a message body. The message header contains fields that represent the total length of the message (or message body length). Usually the design idea is to use int to represent the total length of the message in the first field of the message header; (I used this as I used in Linux C development before).

4. More complex application layer protocols;

The above summary of the reasons for packet loss based on TCP communication (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.