什么时候应该使用 TCP_NODELAY,什么时候应该使用 TCP_CORK? [英] When should I use TCP_NODELAY and when TCP_CORK?

查看:36
本文介绍了什么时候应该使用 TCP_NODELAY,什么时候应该使用 TCP_CORK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道他们都禁用了 Nagle 的算法.

I understood that both of them disable Nagle's algorithm.

我什么时候应该/不应该使用它们中的每一个?

When should/ shouldn't I use each one of them?

推荐答案

首先,不是两者都禁用 Nagle 算法.

First of all not both of them disables Nagle's algorithm.

Nagle 的算法用于减少更多数量的小网络数据包.算法是:如果数据小于限制(通常是MSS),则等待直到收到先前发送的数据包的ACK,同时累积来自用户的数据.然后发送累积的数据.

Nagle's algorithm is for reducing more number of small network packets in wire. The algorithm is: if data is smaller than a limit (usually MSS), wait until receiving ACK for previously sent packets and in the mean time accumulate data from user. Then send the accumulated data.

if [ data > MSS ]
    send(data)
else
    wait until ACK for previously sent data and accumulate data in send buffer (data)
    And after receiving the ACK send(data)

这将有助于 telnet 等应用程序.但是,等待 ACK 可能会增加发送流数据时的延迟.此外,如果接收方实施延迟确认策略",则会导致临时死锁情况.在这种情况下,禁用 Nagle 算法是更好的选择.

This will help in applications like telnet. However, waiting for the ACK may increase latency when sending streaming data. Additionally, if the receiver implements the 'delayed ACK policy', it will cause a temporary deadlock situation. In such cases, disabling Nagle's algorithm is a better option.

因此 TCP_NODELAY 用于禁用 Nagle 算法.

So TCP_NODELAY is used for disabling Nagle's algorithm.

TCP_CORK 积极积累数据.如果在套接字中启用 TCP_CORK,则在缓冲区填充到固定限制之前,它不会发送数据.与 Nagle 的算法类似,它也从用户那里累积数据,但直到缓冲区填充到固定限制,直到收到 ACK.这在发送多个数据块时很有用.但是在使用 TCP_CORK 时你必须更加小心.

TCP_CORK aggressively accumulates data. If TCP_CORK is enabled in a socket, it will not send data until the buffer fills to a fixed limit. Similar to Nagle's algorithm, it also accumulates data from user but until the buffer fills to a fixed limit not until receiving ACK. This will be useful while sending multiple blocks of data. But you have to be more careful while using TCP_CORK.

直到 2.6 内核,这两个选项是相互排斥的.但是在以后的内核中,它们可以同时存在.在这种情况下,将优先考虑 TCP_CORK.

Until 2.6 kernel, both of these options are mutually exclusive. But in later kernel, both of them can exist together. In such case, TCP_CORK will be given more preference.

参考:

这篇关于什么时候应该使用 TCP_NODELAY,什么时候应该使用 TCP_CORK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆