11637 views|0 replies

661

Posts

18

Resources
The OP
 

19- TCP protocol (Nagle) [Copy link]

The special feature of the unp/protocol/tools/winclient/echo_cli.cpp program we used earlier is that it always sends a small packet (TCP segment, only 41 bytes) to the server. Such a small packet is called a tinygram in English. In a good network, such as a local area network, it usually does not cause any trouble. However, in a wide area network, such a small packet may increase the possibility of network congestion. In order to reduce the number of such tinygrams in the network, the Nagle algorithm is used by default in the TCP protocol stack. 1. Nagle algorithm The Nagle algorithm requires: There can be at most one unconfirmed and incomplete small packet on a TCP connection, and no other packets can be sent before it reaches the destination. Before the previous small packet reaches the destination, that is, before its ack is received, TCP will collect the subsequent small packets. When the ack of the previous small packet is received, TCP will merge the collected small packets into a large packet and send it out. In a wide area network, the network delay is generally large. After a small packet is sent out, it may take a long time to receive an ack. Therefore, before receiving an ack, the sender may accumulate many unsent small packets. Write a picture description here Figure 1 How the Nagle algorithm handles small packets In Figure 1, the client first sent a character l to the server. Before receiving the server's response, the client sent three more packets. According to the Nagle algorithm rules, these small packets cannot be sent out before receiving an ack. After receiving the ack, TCP merges the three small packets into one and sends it out at once. 2. Experiment 1 (Enable Nagle algorithm) The Nagle algorithm is enabled by default. In Figure 1, assuming that the round-trip time is 16ms, it is almost impossible to send 4 characters l, o, v, e continuously in these 16ms. This means that our typing speed must exceed about 250 per second, and at least 2 characters must be sent in 16 ms, and the typing speed must exceed 60 per second. So how to simulate the fast sending of characters in the experiment? The client echo_client.cpp provides an option that helps us send a character multiple times in a row when typing a character. For example, if you type the character x, echo_client.cpp will send x many times in a very short time. Client path: unp/protocol/tools/winclient/echo_client.cpp, deployed on Windows. Server path: unp/protocol/tools/tcpserver/echo_serv.c, deployed on Linux. 2.1 Experimental steps Start the server echo_serv on Linux $ ./echo_serv 192.168.80.130 8000 1 Open OmniPeek on Windows to capture packets Start echo_client.exe on Windows // Note that a parameter 5 is added after echo_client, which means that the input characters will be sent 5 times in a row echo_client.exe 192.168.80.130 8000 5 1 2 Next, I typed a character x in the client and pressed the q key to end. 2.2 Packet capture results Write the picture description here Figure 2 Data captured by OmniPeek In Figure 2, the client first sent a character x and then waited for ack. During this period, the client requested to send four more x. TCP collected these small packets later. After receiving ack, it merged these 4 x into one packet and sent it out at once. 3. Experiment 2 (turn off Nagle algorithm) This time we turn off the Nagle algorithm. The difference from experiment 1 is that when the client is started, add another parameter, NONAGLE. // Start the client echo_client.exe 192.168.80.130 8000 5 NONAGLE 1 2 3 Similarly, enter a character x in the client, and the client will help us send x 5 times in a row, and then press q to exit. The packet capture results are as follows: Write a picture description here Figure 3 The result of the client sending data without the Nagle algorithm As can be seen from Figure 3, the 5 data packets contained in the red box were sent out in a very short time. After sending the first packet, TCP did not wait for the other party to send back the confirmation. 4. Summarize and master the rules of the Nagle algorithm What is the difference between the Nagle algorithm and the non-Nagle algorithm?

This post is from Embedded System

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list