gpt4 book ai didi

c - listen() 忽略积压参数?

转载 作者:IT王子 更新时间:2023-10-29 00:14:45 25 4
gpt4 key购买 nike

我有以下问题:

我有 sockfd = socket(AF_INET, SOCK_STREAM, 0)

在我设置并绑定(bind)套接字后(假设使用 sockfd.sin_port = htons(666)),我立即执行:

listen(sockfd, 3);

sleep(50); // for test purposes

我睡了 50 秒来测试积压参数,这似乎被忽略了,因为我可以在端口 666 上建立连接*超过 3 次。

*:我的意思是,对于从客户端发送的每个第 N 个 SYN (n>3),我都会得到一个 syn/ack 并放入监听队列,而不是被丢弃。有什么问题吗?我阅读了 listen(2) 和 tcp(7) 的手册页并发现:

The behavior of the backlog argument on TCP sockets changed with Linux 2.2. Now it specifies the queue length for completely established sockets waiting to be accepted, instead of the number of incomplete connection requests. The maximum length of the queue for incomplete sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog. When syncookies are enabled there is no logical maximum length and this setting is ignored. See tcp(7) for more information.

,但即使使用 sysctl -w sys.net.ipv4.tcp_max_syn_backlog=2sysctl -w net.ipv4.tcp_syncookies=0,我仍然得到相同的结果结果!我一定是遗漏了什么或者完全误解了 listen() 的积​​压目的。

最佳答案

listen() 的 backlog 参数只是建议性的。

POSIX says :

The backlog argument provides a hint to the implementation which the implementation shall use to limit the number of outstanding connections in the socket's listen queue.

当前版本的 Linux 内核将其四舍五入为下一个最高的 2 次方,最小为 16。相关代码在 reqsk_queue_alloc() 中。 .

关于c - listen() 忽略积压参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5111040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com