在 C 中设置 TCP 接收窗口并在 Linux 中使用 tcpdump

发布于 2024-08-20 15:32:57 字数 982 浏览 9 评论 0原文

我正在运行一个运行 2.6.9-55.ELsmp、x86_64 的 Linux 机器。

我尝试使用 C 语言的setsockopt() 函数来设置TCP 接收窗口。我尝试以下操作:

rwnd = 1024;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&rwnd, sizeof(rwnd));

上面的代码段位于从服务器接收数据的客户端程序中。当我启动程序来接收并观察 tcpdump 输出时,我观察到窗口协商,如下所示:

11:34:40.257755 IP clientReceiver.42464 > serverSender.8991: 
S 1742042788:1742042788(0) win 5840 
<mss 1460,sackOK,timestamp 1688222886 0,nop,wscale 2>

我们看到客户端程序实际上正在协商一个与我在客户端程序中设置的窗口不同的窗口。然而,从我如何解释 Steven 的文本(“TCP/IP 图解,第 1 卷”)第 20.4 节,我相信您使用我使用的 setsockopt() 调用来影响他在第 20.4 节第二个块引用中提到的内容(见上文) )。

我想了解我哪里出了问题。

也许我对史蒂文斯所说的话的解释是不正确的。在这种情况下,您能否指出设置接收缓冲区大小的正确方法?为了证明我的困惑,我参考了 Linux TCP 套接字手册页 http://linux.die .net/man/7/tcp(请参阅 SO_RCFBUF 的评论)。

我在这个故事中错过了什么?如何控制接收缓冲区大小(并将其显示在 tcpdump 输出中)?请注意,我在这里提到了套接字选项 SO_RCFBUF 的设置——我知道这就是 SYN 窗口协商中显示的内容。

任何意见都会受到赞赏。

I am running a Linux box running 2.6.9-55.ELsmp, x86_64.

I am trying to set the TCP receive window by using the setsockopt() function using C. I try the following:

rwnd = 1024;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&rwnd, sizeof(rwnd));

The code segment above is in a client program that receives data from a server. When I kick off the program to receive and observe tcpdump output, I observe window negotiation like so:

11:34:40.257755 IP clientReceiver.42464 > serverSender.8991: 
S 1742042788:1742042788(0) win 5840 
<mss 1460,sackOK,timestamp 1688222886 0,nop,wscale 2>

We see that the client program is in fact negotiating a window different from what I have set in the client program. However, from how I can interpret Steven's text ("TCP/IP Illustrated, Volume 1") Section 20.4, I believe you effect what he refers to in the second block quote in Section 20.4 using the setsockopt() call I use (see above).

I'd like to understand where I have gone wrong.

Perhaps my interpretation of what Stevens is saying is incorrect. In that case, could you point me to the correct way of setting the receive buffer size? As a proof of my confusion, I refer the Linux TCP sockets man page at http://linux.die.net/man/7/tcp (see comment on SO_RCFBUF).

What am I missing in this story? How do I control the receive buffer size (and have it show in the tcpdump output)? Please note that I allude to here a setting of the socket option SO_RCFBUF--I understand that's what shows up in the window negotiation in the SYN.

Any input is appreciated.

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

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

发布评论

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

评论(3

暖心男生 2024-08-27 15:32:57

您还需要使用 TCP_WINDOW_CLAMP

rcvbuf = 2048;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)& rcvbuf, sizeof(rcvbuf));
clamp = 1024;
setsockopt(sock, SOL_SOCKET, TCP_WINDOW_CLAMP, (char *)& clamp, sizeof(clamp));

请注意,rcvbuf 是钳位的两倍,也可能更多。你可以让它自动调谐,窗夹仍然可以工作。这不是便携式的。

You need to also use TCP_WINDOW_CLAMP

rcvbuf = 2048;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)& rcvbuf, sizeof(rcvbuf));
clamp = 1024;
setsockopt(sock, SOL_SOCKET, TCP_WINDOW_CLAMP, (char *)& clamp, sizeof(clamp));

Note the rcvbuf is twice the clamp, it could be more. You can let it autotune, the window clamp will still work. This isn't portable.

無處可尋 2024-08-27 15:32:57

接收缓冲区大小只能在连接套接字之前减少 - 您可以随时增加它。相对于 connect() ,您调用 sockopt() 的顺序是什么?

The receive buffer size can be reduced only before you connect the socket - you can increase it at any time. What order are you calling sockopt() in relative to connect()?

痴情 2024-08-27 15:32:57

对于TCP,rwnd值将在recv期间传递。

接收(袜子,buf,rwnd,0);

这将接收 1024 字节。

For TCP, the rwnd value is to be passed during recv.

recv(sock, buf, rwnd, 0);

This shall receive 1024 bytes.

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