关于 Winsock Kernel Buffer 和 Nagle 算法的疑问 [英] Doubt regarding Winsock Kernel Buffer and Nagle algorithm

查看:30
本文介绍了关于 Winsock Kernel Buffer 和 Nagle 算法的疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While reading this article, I got a doubt.

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

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.

这是否意味着如果使用 SO_SNDBUF 选项将 Winsock Kernel Buffer 设置为零,Nagle 算法会被禁用吗?

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

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

If not then where does WINSOCK cache the small data?

推荐答案

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

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

为了优化应用层的性能,Winsock 将数据缓冲区从应用发送调用复制到 Winsock 内核缓冲区.然后,堆栈使用自己的启发式算法(例如 Nagle 算法)来确定何时真正将数据包放到网络上.

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.

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

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

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

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.

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

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.


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


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.

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

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).

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

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).

这篇关于关于 Winsock Kernel Buffer 和 Nagle 算法的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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