在 C 中设置 TCP 接收窗口并在 Linux 中使用 tcpdump [英] Setting TCP receive window in C and working with tcpdump in Linux

查看:33
本文介绍了在 C 中设置 TCP 接收窗口并在 Linux 中使用 tcpdump的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试使用 C 的 setsockopt() 函数设置 TCP 接收窗口.我尝试以下操作:

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

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

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>

我们看到客户端程序实际上正在协商一个与我在客户端程序中设置的窗口不同的窗口.但是,从我如何解释史蒂文的文本(TCP/IP 插图,第 1 卷")第 20.4 节,我相信您会使用我使用的 setsockopt() 调用影响他在第 20.4 节的第二个块引用中所指的内容(见上文)).

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.

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

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

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

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.

感谢任何输入.

推荐答案

你还需要使用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 是钳位的两倍,可能更多.你可以让它自动调谐,窗夹仍然可以工作.这不是便携式的.

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.

这篇关于在 C 中设置 TCP 接收窗口并在 Linux 中使用 tcpdump的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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