简体   繁体   English

listen()在c中的socket编程中的队列长度?

[英]listen() queue length in socket-programing in c?

I have written two pair of codes( server.c and client.c ) in Linux. 我在Linux中编写了两对代码( server.cclient.c )。 One for UNIX-domain AF_UNIX other for INTERNET-domain AF_INET . 一个用于UNIX域AF_UNIX其他用于INTERNET域AF_INET Both are working fine! 两者都工作正常!

listen() is called for backlog queue length = 3 in both servers both servers调用backlog队列长度= 3的 listen()

listen(sockfd, 3);  

In UNIX domain (AF_UNIX): While one client is connected with server, If I try to connect more clients to server. 在UNIX域(AF_UNIX)中:当一个客户端与服务器连接时,如果我尝试将更多客户端连接到服务器。 Three are kept in queue, and request of fourth is declined. 三个被排队,第四个的请求被拒绝了。 (as I desired - 3 in waiting queue). (正如我所希望的 - 等待队列中的3个)。

In INTERNET domain (AF_INET): Request of more than three are kept in a pending queue. 在INTERNET域(AF_INET)中:超过三个的请求保留在待处理队列中。

Why isn't a request from a fourth client rejected, even when the backlog queue length is three? 即使积压队列长度为3,为什么拒绝来自第四个客户端的请求? And why is the behavior of listen() (and others) protocol dependent? 为什么listen() (和其他)协议的行为依赖?

Operating systems actually use larger queues for incoming TCP connections than the one specified to listen() . 对于传入的TCP连接,操作系统实际上使用的队列大于listen()指定的队列。 How much larger depends on the operating system. 多大程度取决于操作系统。

 listen(int socket_fd, int backlog)  

For a given listening socket kernal maintains two queue. 对于给定的侦听套接字,kernal维护两个队列。

  1. An incomplete connection queue - for which SYN has been come but three-way handshaking (TCP) is not done completely. 一个不完整的连接队列 - 已经发生了SYN但是三方握手(TCP)并没有完全完成。 (SYN_RCV state) (SYN_RCV状态)
  2. A complete connection queue - Three-way handshaking done. 完整的连接队列 - 完成三方握手。 (ESTABLISHED state) (已建立的州)

backlog argument historically specify sum of both queues. backlog参数历史上指定两个队列的总和。 But there is no formal definition of what backlog means. 积压的含义并没有正式的定义。

Berkeley-derived implementation add a fudge factor to the backlog. Berkeley派生的实现为积压添加了一个软糖因素。 So total queue length = factor * backlog . 所以总队列length = factor * backlog

A very detailed and deep explanation given in a book by W. Richard Stevens. W. Richard Stevens在一本书中给出的非常详细和深刻的解释。 Also a table showing the values for seven operating systems can be found in Stevens, Fenner, Rudoff, " Unix Network Programming: The Sockets Network API ", Volume 1, Third Edition, Page 108. 此外,还可以在Stevens,Fenner,Rudoff,“ Unix网络编程:套接字网络API ”,第1卷,第3版,第108页中找到显示7个​​操作系统的值的表。

The platform is entitled to adjust the specified backlog up or down, according to its minimum and its default. 平台有权根据其最小值和默认值向上或向下调整指定的积压。 These days the default is more like 500 than five, which is where it started in about 1983. You can't rely on it being what you specified, and there is no API for finding out what it really is, and there is no apparent valid application reason for wanting it to be shorter than the default. 这些天的默认值更像是500比五,这是它在大约1983年开始的地方。你不能依赖它是你指定的,并且没有API来找出它到底是什么,并且没有明显的希望它比默认值短的有效应用程序原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM