Resolving the "Sticky pack" problem with TCP network transport

Source: Internet
Author: User
Tags error code pack socket

At present, the TCP/IP communication protocol and its standard socket application Development Programming Interface (API) are widely used in network transmission applications. The TCP/IP transport layer has two parallel protocols: TCP and UDP. Where TCP (Transport Control Protocol, Transmission Control Protocol) is connection-oriented, it provides high reliability services. UDP (User Datagram Protocol, Subscriber Datagram Protocol) is connectionless, providing efficient service. In practical engineering applications, the choice of reliability and efficiency depends on the application environment and requirements. In general, the network transmission of ordinary data uses the high efficiency UDP, the important data network transmission uses the highly reliable TCP.

In the process of application development, the author finds that applications that are based on TCP network transmission sometimes have sticky packets (that is, packets sent by the sender are glued to a packet when received by the receiver). In view of this situation, we have carried on the special research and the experiment. This paper focuses on the analysis of the problem of TCP network sticky, and combined with the results of the experiment to solve the problem of the countermeasures and methods for the relevant engineering and technical personnel for reference.

Introduction of TCP protocol

TCP is a connection-oriented transport layer protocol, although TCP does not belong to ISO set of protocols, but because of its successful application in business and industry, it has become a de facto network standard, which is widely used in communication between various network hosts.

As a connection-oriented transport layer protocol, the goal of TCP is to provide users with reliable end-to-end connectivity to ensure an orderly and accurate transmission of information. In addition to providing basic data transmission functions, it also uses a series of measures, such as number, calibration, data validation and so on to ensure reliability. It numbers each byte of data that is transmitted and requests the receiving party to return the acknowledgment information (ACK). If the sender does not receive a confirmation of the data within the specified time, it will retransmit the data. The data number enables the receiver to handle the problem of disorder and repetition of the data. The problem of data error rate is solved by increasing the checksum in each transmitted data segment, the receiver checks the checksum after receiving the data, and if the checksum is incorrect, discards the data segment of the error code and requests the sender to retransmit it. Flow control is also an important measure to ensure reliability, if no flow control, may be due to receive buffer overflow loss of a large number of data, resulting in a lot of retransmission, resulting in a vicious circle of network congestion. TCP uses a variable window for traffic control, and the receiver controls the amount of data sent by the sender.

TCP provides high reliability network transmission service for users, but reliability guarantee measures also affect transmission efficiency. Therefore, in the practical engineering application, only the key data transmission uses the TCP, but the ordinary data transmission generally uses the high efficiency UDP.

Analysis and countermeasure of sticky bag problem

A TCP sticky packet is a packet of data sent by the sender to the receiver when it is received, and from the receiving buffer, the header of the last packet of data is followed by the end of the previous packet of data.

There are many reasons for the sticky phenomenon, which may be caused by the sender or by the receiver. The sticky packet caused by the sender is caused by the TCP protocol itself, TCP to improve transmission efficiency, the sender often collect enough data before sending a packet of data. If the data is sent several times in a row, TCP will usually synthesize the data into a packet after the optimization algorithm is sent, so that the recipient receives the sticky packet data. The sticky packet caused by the receiver is caused by the user process of the receiver not receiving the data in time, causing the sticky packet phenomenon. This is because the receiver first places the received data in the system receive buffer, the user process takes data from the buffer, and if the previous packet of data has not been taken away by the user process when the next packet arrives, the next packet of data is placed in the system receive buffer and the previous packet of data is received. The user process receives the data from the system by receiving the buffer from the preset buffer size, so that the packet data is fetched at a time (Figure 1).

Figure 1

Figure 2

Figure 3

There are two kinds of sticky bags, a package that is glued together is a complete package (shown in Figure 1, Figure 2), and the other is that the stuck package has an incomplete package (shown in Figure 3), where the user receives the buffer length of M bytes.

Not all packaging phenomena need to be processed, if the transmission of data for the continuous flow of data (such as file transfer), you do not have to put the adhesion of the packet separated (for short, subcontracting). But in the practical engineering application, the data that transmits is generally the data with structure, then need to do subcontract processing.

When dealing with the sticky problem of fixed-length structure data, the subcontracting algorithm is simple, and the subcontract algorithm is more complex when dealing with the problem of viscous packets with indefinite long structure data. In particular, as shown in Figure 3, the sticky package, because a packet of data content is divided into two consecutive receiving packets, it is difficult to deal with. In practical engineering application, it is necessary to avoid the phenomenon of sticky packaging.

In order to avoid the sticky phenomenon, the following measures can be taken. One is for the sender caused by the phenomenon of sticky packets, the user can avoid by the programming setting, TCP provides the operation instruction that forces the data to send immediately push,tcp the software receives the operation instruction, immediately sends this data out without waiting for the sending buffer to fill, and the second is the sticky package caused by the receiver, Can be optimized by programming, streamline the receiving process workload, improve the receiving process priority and so on, so that it receives data in time, so as to avoid the phenomenon of sticky packets; third, by the receiver control, a packet of data by the structure of the field, the artificial control of multiple received, and then merged, through this means to avoid sticky packets

The three measures mentioned above have their drawbacks. The first programming setting method avoids the sticky packets caused by the sender, but it turns off the optimization algorithm, reduces the efficiency of the network transmission, and affects the performance of the application, which is generally not recommended. The second method can only reduce the likelihood of sticking, but can not completely avoid the sticky packet, when the sending frequency is high, or because the network burst may make a time packet to reach the receiver faster, the receiver is still likely to be too late to receive, resulting in sticky packets. The third method avoids the sticky package, but the application is less efficient and unsuitable for real-time applications.

A more comprehensive approach is: the receiver to create a preprocessing thread, docking received packets for preprocessing, the adhesion of the packet separated. We have experimented with this method and proved to be efficient and feasible.

Third, programming and implementation

1. Implementation Framework

The Experimental network communication program is implemented by the socket API of TCP/IP protocol. The socket is oriented to the client/server model. The TCP implementation framework is shown in Figure 4.

Figure 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.