Tcpip (16) TCP interactive data stream

Source: Internet
Author: User

TCP data streams can be roughly divided into two types: Interactive Data streams and block data streams. Interactive Data streams are usually small, while block data streams are usually full (512 bytes ). When processing two types of data, TCP has different algorithms.

Data corruption with Ack

For interactive input, a possible key echo method is as follows:

In the figure, the confirmation of the data bytes in the middle and the echo of the data bytes can be combined and sent, that is, the confirmation of the delay (data includes ACK) can be combined.

Data includes ack: TCP does not immediately send ACK when receiving data, but delays sending so that ack can be sent together with the data to be sent in this direction, generally, the implementation latency is 200 ms.

Nagle Algorithm

As we can see above, each time the two sides of TCP interaction send data (even if only one byte of data), they need to generate a (Data Length + 40 bytes) group. When the data length is far less than 40 bytes, the actual network utilization is actually very low, and a large number of small groups will also increase the possibility of congestion.

The Nagle algorithm solves this problem. It requires that a TCP connection can have at most one unconfirmed unfinished small group, and cannot send other small groups before the confirmation of this group arrives. TCP collects these small groups and sends them out in the form of a group upon confirmation. The faster the confirmation arrives, the faster the data is sent, and fewer groups can be sent.

The Nagle algorithm is enabled by default to optimize packet sending during TCP connections. Network Transmission is optimized to Ensure network latency and network congestion.

Although Nagle solves the small packet problem, it also causes a high unpredictable delay and reduces the throughput. 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. Try to transmit the group package of data to the maximum MTU. If the size of the sent packet is too small ~ If no MTU is assembled within S, it is directly sent. If the size of the sent data packet is within 0.45, an MTU is assembled each time for sending. If the interval is greater than 0.4 ~ 0.8s, each packet is sent directly.

Tcp_nodelay Option

By default, the negale algorithm is used to send data. In this way, although the network throughput is improved, the real-time performance is reduced. In some highly interactive applications, the negale algorithm can be disabled using the tcp_nodelay option.

In this case, each packet sent by the application to the kernel is immediately sent. Although the negale algorithm is disabled, the network transmission is still affected by the TCP validation delay mechanism.

Tcp_cork Option

The so-called Cork is the meaning of the plug-in, and the image is to use the cork to plug the connection, so that the data is not sent out, and then sent out after the plug-in is pulled out. After this option is set, the kernel will try its best to splice a small data packet into a large data packet (One MTU) and then send it out. Of course, if after a certain period of time (generally 200 ms, this value has yet to be confirmed). When the kernel is still not combined into an MTU, the existing data must also be sent (it is impossible to keep the data waiting ).
However, the implementation of tcp_cork may not be as perfect as you think, and cork will not completely plug the connection. The kernel does not know when the application layer will send the second batch of data for splicing with the first batch of data to reach the MTU size. Therefore, the kernel will give a time limit, if you do not splice a large package (try to get close to MTU) during this time, the kernel will send it unconditionally. That is to say, if the application layer program does not send small packets of data at short intervals, tcp_cork will have no effect, but will lose the real-time data (each small packet of data will be delayed for a certain time before sending ).

Nagle algorithm andCork algorithm difference

The Nagle algorithm and the cork algorithm are very similar, but their focus is different. The Nagle algorithm mainly avoids network congestion due to too many packets (the proportion of protocol headers is very large, the cork algorithm aims to improve the network utilization and minimize the proportion of protocol headers in general. In this case, the two are the same in avoiding sending packets. At the user control level, the Nagle algorithm is not controlled by the user socket. You can only simply set tcp_nodelay and disable it, the cork algorithm also enables or disables tcp_cork by setting or clearing tcp_cork. However, the Nagle algorithm is concerned with network congestion issues, and packets are sent when all Ack is returned, however, the cork algorithm can focus on the content. It is very important to ensure that the frontend and backend packets are sent at a short interval (otherwise, the kernel will send scattered packets to you ), even if you send multiple small data packets in a distributed manner, you can use the cork algorithm to splice the content into a package. If you use the Nagle algorithm at this time, you may not be able to do this.

 

Refer:

  • Http://www.cnblogs.com/zhaoyl/archive/2012/09/20/2695799.html
  • Http://hi.baidu.com/wy_51131/item/6a6ccfd6a2e884846dce3fbd
  • TCP/IP details

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.