TCP options: tcp_nodelay and tcp_cork

Source: Internet
Author: User

Nagle algorithm tcp_nodelay and tcp_cork

Nagle Algorithm
According to the name of the creator John Nagle. This algorithm is used to automatically connect a certain number of messages in the buffer zone. This process

(Nagling) improves the efficiency of network application systems by reducing the number of packets that must be sent. Nagle Algorithm

Controlled by Ford aerospace and Communications Corporation congestion in IP/tcp

Internetworks (ietf rfc 896) (1984) was initially used to buffer Ford's private TCP/IP network congestion, not

Has been widely spread.

The Nagle document defines a solution to the small packet problem. When an application generates only one byte at a time

As a result, the network is overloaded due to such a small packet (in this case, it is often referred to as "sending Sb window complications"), from

This problem occurs. A single character from the keyboard-1 byte of data-may cause a 41-byte packet to be transmitted.

The packet contains 1 byte of useful data and 40 byte of header data. In this 4000% overload situation, only

It is acceptable in a network with low load, but in a network with heavy load such as Ford, it may force re-transmission, resulting in

Packet Loss reduces transmission speed through over-crowded switching nodes and gateways. Further, when the connection is discarded, the throughput

May be reduced. Nagle algorithm-the common implementation method is to insert two lines of code in a TCP Program-on the sender

Buffer (store) the data that does not respond (this strange sentence is actually to buffer unsent data in order, in

Splicing at sending ). The data sent in sequence will be kept to the response that receives the identified data or a whole package of valuable data.

To be sent.

Although the Nagle algorithm is used to solve problems in the Ford network, the same problems also occur in apranet. Through the network,

Nagling is widely implemented, including the Internet, and has a huge effect-although sometimes in highly interactive environments such

In some C/S cases, you do not want to perform the processing. In this case, you can use the tcp_nodelay socket option to disable nagling.

Note: Although Nagle solves the small packet problem, it also causes a high unpredictable delay and reduces the throughput.

In fact, you can implement the following Nagle algorithm by yourself. In fact, the Nagle algorithm is not very complex. His main responsibilities

It is the accumulation of data. In fact, there are two thresholds: one is that the number of bytes in the slow-forward area reaches a certain level, and the other is waiting.

A certain period of time (generally, the Nagle algorithm waits for 200 ms); any one of the two thresholds must send data.

. Generally, if the data traffic is large, the second condition will never work. However, when a small packet is sent,

The second threshold is used to prevent data from being infinitely cached in the buffer zone. Learned about TCP Nagle Calculation

Then we can implement a similar algorithm by ourselves. Before doing so, we need to remember an important thing.

It is also the main motivation for us to implement the Nagle algorithm. I want to send data urgently, so

In addition to the above two thresholds, an additional threshold is for sending emergency data. Now we can start working. Here we mainly

Train of Thought:

First, we must establish another layer on the socket to define our own transmission control. Our Nagle algorithm is also in

Implemented in this layer.
Which TCP Nagle algorithm is disable?
Use the select function to check whether data can be sent. Of course, we need to add data to the fd_set

The three thresholds are checked according to the bytes and urgent data. In general, the two conditions are met, and then according

Time. We use a cumulative byte counter and a wait time timer. The Accumulate byte counter is added at each time

When the data is added to our control layer, it is accumulated. After the data is sent, the number of bytes in the response is subtracted.

When the data is submitted to the control layer (you can use Windows gettickcount to get the current time), and then

Reset each time the data is sent.
In fact, the Nagle algorithm has been implemented in this way, and the system performance is reduced without calling gettickcount frequently.

Tcp_cork

The Nagle algorithm is enabled by default to optimize packet sending during TCP connections. Optimized network transmission, with both network latency and network

Network congestion. In this case, you can set tcp_nodelay to disable the Nagle algorithm. If data packets exist, they are directly sent to ensure network timeliness.

When sending a large amount of data, you can set tcp_cork to disable the Nagle algorithm to Ensure network availability. Data as much as possible

The group package is transmitted to the maximum MTU. If the size of the sent packet is too small ~ Within the 0.8s range, none of them can be assembled into one.

Send the message directly. If the size of the sent data packet is enough to be within 0.45, an MTU is assembled each time for sending. If

Interval greater than 0.4 ~ 0.8s, each packet is sent directly.

From: http://blog.csdn.net/lin49940/archive/2009/07/26/4382303.aspx
Tcp_nodelay Option

Set this option: Public void settcpnodelay (Boolean on) throws socketexception
Read this option: Public Boolean gettcpnodelay () throws socketexception
By default, the negale algorithm is used to send data. The negale algorithm means that the data sent by the sender will not be sent immediately,

Instead, it is first placed in the buffer zone and then sent when the cache is full. After sending a batch of data, it will wait for the recipient to respond to this batch of data,

Then the next batch of data is sent. The negale algorithm is suitable for sending large batches of data to the sender, and the receiver will make

In response, this algorithm increases communication efficiency by reducing the number of data transfers.

Negale

The algorithm causes the sender to run slowly. For GUI programs, such as online games, the server needs to track the mouse movement of the client in real time.

This problem is particularly prominent. The information changed by the client's mouse position needs to be sent to the server in real time, due to the negale Algorithm

Buffer is used, which greatly reduces the real-time response speed and causes the program to run slowly.

Below from http://hi.baidu.com/dirlt/blog/item/5ea4be1795d30b03c83d6d7f.html

Tcp_nodelay and tcp_cork

Here, let's get to know the background of the problem. [skip sliding windows, just talking about the packet organization.]
1. In history, TCP waits for an ACK every time a packet is sent and

2. But in some interactive applications such as telnet, the result is that each time we press the key, a packet is sent.

It is not efficient to match a TCP header, and the Nagle algorithm came out. When the sending method sends data to a and then waits for the ACK from the recipient,

Accumulate all TCP packets collected locally and send them at one time. But it is obvious that the Nagle algorithm is not conducive to interactive scenarios.

3. But there are still interactive applications under modern applications, so sometimes we need to disable Nagle so we can set

Tcp_nodelay

4. However, the length of the Nagle Organization Package is determined by the system. Sometimes we know that we will generate 1 byte every 1 minute, totaling 1000 bytes.

Bytes. If it is completely sent by the Nagle algorithm, it may still be sent in 1 byte and 1 byte. [This is an extreme situation. Assume that

Ack return time is not very long]. In this case, set tcp_cork to block TCP [try to block] and wait until we finish writing

After 1000 bytes, cancel tcp_cork. At this time, the 1000 bytes can be sent once.
Tcp_nodelay and tcp_cork both disable the Nagle algorithm, but nodelay is completely disabled, while tcp_cork is completely determined by itself.

Sending time. In the Linux document, do not set both.

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.