关于Winsock Kernel Buffer和Nagle算法的疑问

发布于 2024-07-24 22:36:58 字数 328 浏览 9 评论 0原文

在阅读这篇文章时,我有一个疑问。

据我了解,在传输小数据时,默认情况下会启用 Nagle 算法,该算法会合并小数据包。 这会导致在传输之前缓存一些数据。 我相信 Winsock Kernel Buffer 是缓存发生的地方。 如果我错了请纠正我。

这是否意味着如果使用 SO_SNDBUF 选项将 Winsock 内核缓冲区设置为零,Nagle 算法是否会被禁用?

如果不是那么WINSOCK将小数据缓存在哪里?

While reading this article, I got a doubt.

I understood that while trasferring small data, Nagle algorithm is enabled by default which coalesces small packets. This results in caching some data before transmission. I believe that Winsock Kernel Buffer is the place where caching happens. Correct me if I am wrong.

Does it mean that if the Winsock Kernel Buffer is set to zero with SO_SNDBUF option, will the Nagle algorithm be disabled?

If not then where does WINSOCK cache the small data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-07-31 22:36:58

您引用的知识库文章以这种方式给出了您的答案......

为了优化应用程序层的性能,Winsock 将数据缓冲区从应用程序发送调用复制到 Winsock 内核缓冲区。 然后,堆栈使用自己的启发式方法(例如 Nagle 算法)来确定何时实际将数据包放到线路上。

并且,设置 TCP_NODELAY 或 SO_SNDBUF=0 将禁用 Nagle 算法,如下所示,

应用TCP_NODELAY套接字选项来禁用Nagle算法,以便将小数据包无延迟地传送到远程主机。

您可以使用 SO_SNDBUF 选项更改分配给套接字的 Winsock 内核缓冲区的大小(默认为 8K)。 如有必要,Winsock 可以缓冲明显大于 SO_SNDBUF 缓冲区大小的内容。 大多数情况下,应用程序中的发送完成仅表明应用程序发送调用中的数据缓冲区已复制到 Winsock 内核缓冲区,并不表明数据已到达网络介质。 唯一的例外是通过将 SO_SNDBUF 设置为 0 来禁用 Winsock 缓冲


阅读下面的评论,我意识到您可能会感到困惑,因为设置 TCP_NODELAY 或设置 SO_SNDBUF=0 似乎都在做同样的事情。 如果是这种情况,请注意 Nagle 仅适用于 TCP 流(将数据分段为数据包),而 SO_SNDBUF 也适用于 UDP 套接字。

将 SO_SNDBUF 设置为零显式停止所有输出缓冲,并尝试立即调度套接字上的每个“写入”(至少在正常套接字实现中)。

设置 TCP_NODELAY 将显式停止 TCP 套接字上的 Nagle 算法,尽管发送缓冲区可能可用并用于延迟调度(在向应用程序确认发送成功后)。

The KB article you refer gives your answers in this way...

To optimize performance at the application layer, Winsock copies data buffers from application send calls to a Winsock kernel buffer. Then, the stack uses its own heuristics (such as Nagle algorithm) to determine when to actually put the packet on the wire.

and, setting TCP_NODELAY or SO_SNDBUF=0 will disable Nagle algorithm as below,

The TCP_NODELAY socket option is applied to disable the Nagle algorithm so that the small data packets are delivered to the remote host without delay.

You can change the amount of Winsock kernel buffer allocated to the socket using the SO_SNDBUF option (it is 8K by default). If necessary, Winsock can buffer significantly more than the SO_SNDBUF buffer size. In most cases, the send completion in the application only indicates the data buffer in an application send call is copied to the Winsock kernel buffer and does not indicate that the data has hit the network medium. The only exception is when you disable the Winsock buffering by setting SO_SNDBUF to 0.


Reading your comment below, I realize you might be confused because setting TCP_NODELAY or setting SO_SNDBUF=0 both seem to be doing the same thing. If that is the case, please note that Nagle is applicable only over TCP streams (which segments data into packets), whereas SO_SNDBUF is a also applicable to UDP sockets.

Setting SO_SNDBUF to zero explicitly stops all output buffering and an immediate dispatch is attempted for each 'write' on the socket (at least in normal socket implementations).

Setting TCP_NODELAY will explicitly stop Nagle algorithm on TCP sockets though the send buffer may be available and used for delayed dispatch (after send success is acknowledged to the application).

飘过的浮云 2024-07-31 22:36:58

SO_SNDBUF 设置为 0 不会强制立即在线发送。

SO_SNDBUF set to 0 does NOT force an immediate send on the wire.

娇俏 2024-07-31 22:36:58

将 SO_SNDBUF 设置为零将不会隐式禁用 nagle; WSK 维护的 nagle 状态与缓冲区所在的位置无关。 有责任保持您发布的缓冲区有效,直到传输消耗它。

Setting SO_SNDBUF to zero will not implicitly disable nagle; the nagle state maintained by the WSK is independent of where the buffer is located. It's your responsibility to keep the buffers you post valid till the transport consumes it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文