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

标签 c linux sockets listen

我有以下问题:

我有 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/44237026/

相关文章:

c# - 从 SocketException 获取实际套接字

c - 无效参数 : connect() C socket programming

c - 堆栈上的局部变量分配顺序

python-3.x - 如何将套接字消息转换为字符串消息

Linux 传入/传出数据包修改模块

python - 在 Linux 中以编程方式检查特定类型的设备

linux - linux系统上的/proc/self/fd/socket文件是什么意思以及如何找到端口

c - 什么会导致 sock send() 命令出现 “Resource temporarily unavailable”

c++ - 使用筛法进行素因数分解

java - 在 Ubuntu 上设置 Java 环境路径时出现问题 (libjvm.so : cannot open shared object file: No such file or directory)