问题描述
我有以下问题:
我有 sockfd = socket(AF_INET, SOCK_STREAM, 0)
在我设置并绑定套接字后(假设使用 sockfd.sin_port = htons(666)
),我立即执行:
After I set up and bind the socket (let's say with sockfd.sin_port = htons(666)
), I immediately do:
listen(sockfd, 3);
sleep(50); // for test purposes
我睡了 50 秒来测试 backlog 参数,这似乎被忽略了,因为我可以在端口 666 上建立连接* 3 次以上.
I'm sleeping for 50 seconds to test the backlog argument, which seems to be ignored because I can establish a connection* more than 3 times on port 666.
*:我的意思是,对于从客户端发送的每个第 N 个 SYN (n>3) 并放置在侦听队列中,而不是被丢弃,我都会获得一个同步/确认.可能有什么问题?我已经阅读了 listen(2) 和 tcp(7) 的手册页,发现:
*: What I mean is that I get a syn/ack for each Nth SYN (n>3) sent from the client and placed in the listen queue, instead of being dropped. What could be wrong? I've read the man pages of listen(2) and tcp(7) and found:
在 Linux 2.2 中,TCP 套接字上的 backlog 参数的行为发生了变化.现在它指定等待接受的完全建立的套接字的队列长度,而不是未完成的连接请求的数量.不完整套接字队列的最大长度可以设置使用/proc/sys/net/ipv4/tcp_max_syn_backlog.当启用了 syncookies 时,没有逻辑最大长度,这设置被忽略.参见 tcp(7)更多的信息.
,但即使使用 sysctl -w sys.net.ipv4.tcp_max_syn_backlog=2
和 sysctl -w net.ipv4.tcp_syncookies=0
,我仍然得到相同的结果结果!我一定是遗漏了什么或者完全误解了 listen()
的 backlog 目的.
, but even with sysctl -w sys.net.ipv4.tcp_max_syn_backlog=2
and sysctl -w net.ipv4.tcp_syncookies=0
, I still get the same results! I must be missing something or completely missunderstand listen()
's backlog purpose.
推荐答案
listen()
的 backlog 参数只是建议性的.
The backlog argument to listen()
is only advisory.
backlog 参数提供了一个提示到实施实施应用于限制中未完成的连接数套接字的监听队列.
Linux 内核的当前版本将其四舍五入为 2 的下一个最高幂,最少为 16.相关代码位于 reqsk_queue_alloc().
Current versions of the Linux kernel round it up to the next highest power of two, with a minimum of 16. The revelant code is in reqsk_queue_alloc().
这篇关于listen() 忽略积压参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!