Troubleshooting TCP network transmission "stick pack" question of: Wang Sheng Yang Xiaoping opened ORIGINAL:
http://www.ciw.com.cn/
Currently in network transmission applications, a TCP / IP communication protocol and its standard Socket Application Development Programming Interface (API) are developed. TCP / IP transport layers have two parallel protocols: TCP and UDP. Where TCP (Transport Control Protocol, Transmission Control Protocol) is connected, providing high reliability services. UDP (User DataGram Protocol, User Data News Agreement) is not connected, providing high efficiency services. In actual engineering applications, the choice of reliability and efficiency depends on the environment and needs of the application. Under normal circumstances, the network transmission of ordinary data uses highly efficient UDP, and the network transmission of important data uses high reliability TCP.
During the application development process, the author finds that the application based on TCP network transmission sometimes occurs (i.e., the number of packet data transmitted by the sender to the receiver to receive a packet). In response to this situation, we conducted special research and experiments. This paper focuses on TCP network adhesive problems, and combines experimental results to solve the countermeasures and methods of this problem, for reference.
First, introduction to TCP protocol
TCP is a connection-oriented transport layer protocol, although TCP does not belong to ISO, it has become a factual network standard due to its successful application in the business and industry, it has been widely used in various network hosts. Communication.
As a connection-oriented transport layer protocol, TCP's goal is to provide a reliable end-to-end connection to ensure orderless transmission. In addition to providing basic data transfer functions, it also uses a series of measures such as data number, check, and data confirmation, etc. to ensure reliability. It is numbered each of the data bytes transmitted and the receiver return confirmation information (ACK) is requested. If the sender does not receive the data confirmation within the specified time, the sender retransmit it. The data number enables the recipient to handle the sequence and repetition of the data. Data error issues are resolved by adding checksum in each transmission data segment, the recipient checks the checksum after receiving the data, if the verification and incorrect error, then discard the data segment of the error, and Require sender retransmission. Flow control is also an important measure to ensure reliability. If there is no fluid control, you may lose a lot of data due to the receiving buffer overflow, resulting in many retransmission, resulting in a vicious circle of network congestion. The TCP uses a variable window to perform flow control, and the receiver controls the amount of data sent by the sender.
TCP provides users with high reliability network transport services, but reliability protection measures also affect transmission efficiency. Therefore, in actual engineering applications, only the transmissions of critical data use TCP, while the transmission of normal data generally uses high efficiency UDP.
Second, the analysis and countermeasures of adhesive bag
The TCP adhesiveb is referred to as a plurality of packet data sent by the sender to the receiver to receive a packet, from the receiving buffer, the head of the rear packet data is adjusted at the end of the previous package data.
The cause of adhesive phenomena is multifaceted, which may be caused by the sender, or may be caused by the recipient. The invasive bond caused by the sender is caused by the TCP protocol itself. TCP is to increase transmission efficiency, and the sender often sends a pack data after receiving sufficient data. If the data sent several times will be very small, typically TCP sends these data according to the optimization algorithm, and then transmits it, so that the recipient receives the adhesive data. The adhesive packet caused by the receiver is due to the reception of data in time, resulting in a patch phenomenon. This is because the receiver first places the received data in the system receiving buffer, and the user process takes data from the buffer. If the next packet data arrives, the front pack data has not been taken away by the user process, then the next package data After receiving the buffer when the system receives the buffer, the user process receives the buffer to take data from the system according to the preset buffer size, so that the multi-package data is taken at a time (shown in Figure 1). figure 1
Figure 2 Figure 3 There are two types of adhesive cases, one is a complete data package (shown in Figure 1, Figure 2), and the other case is incomplete package with incomplete packages (Fig. 3), it is assumed here that the user receives the buffer length of M-bytes.
Not all of the adhesive phenomena need to be handled, and if the transmitted data is a continuous stream of streams without the structure, it is not necessary to open the adhesive package (score). However, in actual engineering applications, the transferred data is generally data-based data, and the sub-packing is required.
When dealing with the adhesive problem of the fixed-length structure data, the bracket algorithm is relatively simple; the subcontracting algorithm is more complicated when dealing with the adhesive problems of unprodul long structure data. In particular, the adhesive case shown in Fig. 3, since the pack data content is divided into two consecutive receiving packets, the difficulty is difficult to process. Package phenomenon should be avoided in the actual engineering application.
In order to avoid adhesive phenomena, the following measures can be taken. First, for the patch phenomenon caused by the sender, the user can avoid the programming settings, the TCP provides the operation command PUSH that is forced to transfer immediately, and the TCP software receives the data, and immediately sends this segment data. It is not necessary to wait for the transmission buffer full; the second is to the adhesive bag caused by the recipient, and the data can be improved by optimizing the program, streamline the progress of the process, improve the priority of the reception process, so that it will receive data to avoid adhesive bags. Phenomenon; third is controlled by the recipient, receive a pack of data multiple times, human control, and then merge, and then to avoid adhesive bags.
The three measures mentioned above have their shortcomings. Although the first programming method can avoid the adhesive bag caused by the sender, it closes the optimization algorithm, reducing network transmission efficiency, affecting the performance of the application, is generally not recommended. The second method can only reduce the possibility of adhesive bags, but it does not completely avoid adhesive bags. When the transmission frequency is high, or due to network bursts may enable a certain time period data package to reach the recipient faster, receiver It is still possible to receive it, resulting in adhesive bags. Think of the third method, although the adhesive bag is avoided, the application has a low efficiency, and the occasion of real-time applications is not suitable.
A comparative weekly countermeasure is that the recipient creates a pre-processing thread that presses the received packet for pre-processing, and the adhesive package is separated. This method we conducted experiments and proved to be efficient.
Third, programming and implementation
1. Implementation framework
The experimental network communication program adopts the Socket API programming of the TCP / IP protocol. Socket is a client / server model. The TCP implementation framework is shown in Figure 4.
Figure 4
2. Experimental hardware environment:
Server: Pentium 350 Microcomputer
Client: Pentium 166 Microcomputer Network Platform: LAN for 10 megabit HUB
3. Experimental software environment:
Operating system: Windows 98
Programming Language: Visual C 5.0
4. Main thread
Programming uses a multi-threaded manner, a total of two threads: Send a data thread, send a statistical display thread. The client has three threads: receives the data thread, receives the pre-processing adhesive thread, and receives the statistical display thread. Where, send and receive thread priority is set to thread_priority_time_critical (highest priority), preprocessing thread priority is Thread_Priority_ABOVE_NORMAL (higher than normal priority), displaying thread priority to thread_priority_normal (ordinary priority).
The data structure of the experiment sends data is shown in Figure 5:
Figure 5
5. Baby algorithm
In response to three different adhesive phenomena, the package algorithm has adopted a corresponding solution. The basic idea is first to force the receiving data stream (length of the length to be processed into a predetermined structural data in the form of a predetermined structure data, and remove the structural data length field, that is, N in Fig. 5, and calculate the first package according to N Data length.
1) If n 3) If n> m, it indicates that the content of the data stream is not enough to form a complete structure data, and it is necessary to leave after the next package data merge. Interested by the specific content and software of the bracket algorithm, can contact the author. Fourth, the experimental results analysis The experimental results are as follows: 1. In the above experimental environment, when the number of packet data lengths of the sender continuously transmits the sum of 1500B, the adhesive phenomenon is often occurred, and the receiving means can correctly unwind the packet that is adhered together after the pre-processing thread. If the "transmission is not delay" is set in the program: (setSockopt (socket_name, ipproto_tcp, tcp_nodelay, (char *) & on, sizeof on), where ON = 1), there is no adhesive phenomenon. 2. When the transmitted data is an unordered length data per pack 1kb ~ 2kb, if the transmission interval is less than 10 ms, the adhesive bag occurs occasionally, the receiver can correctly unwind the packet that is adhered together after the pre-processing thread. 3. To determine the time of handling the adhesive bag, the sender sequentially transmits the length of 1.5 kb, 1.9kb, 1.2kb, 1.6 kb, 1.0 kb, 1.0 kb, 1.6kb, 1.0kb data, a total of 1000 packets. To make the adhesive phenomenon, the receiving thread waits 10ms each time, the receiving buffer is set to 5000B, and the resulting party receives 526 packet data, wherein the length of 5000B has a 175 packet. The pre-processing thread processing can obtain a 1000-package correct data, and the total time of the adhesive package is less than 1 ms. The experimental results show that the TCP adhesive phenomenon does exist, but can be solved by the receiver pretreatment, and the processing time is very short (the total processing time of 1000 packet data in the experiment is less than 1 ms), and there is hardly affecting the normal operation of the application. Author: Recently, I have also encountered TCP issued a problem with TCP, I feel that this article is very good, posted and share with everyone.