listen() 每个 Windows 版本的最大队列大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4709756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
listen() maximum queue size per Windows version
提问by rjobidon
The Winsock function listen(socket, backlog) has a parameter to specify the size of the queue for pending connections. The program should pass SOMAXCONN to set the queue to its maximum size.
Winsock 函数 listen(socket, backlog) 有一个参数来指定待处理连接的队列大小。程序应该通过 SOMAXCONN 将队列设置为其最大大小。
Question: What is the maximum queue size for each Windows version: 2000, XP, Vista, 7?
问题:每个 Windows 版本的最大队列大小是多少:2000、XP、Vista、7?
Thanks!
谢谢!
Reference: listen() on MSDN Library
采纳答案by Len Holgate
What do you hope to achieve once you know the answer to this?
一旦你知道了这个问题的答案,你希望得到什么?
There are many ways to improve the performance of connection acceptance, you should focus on that and not what the actual number of queued connections can be.
有很多方法可以提高连接接受的性能,您应该关注这一点,而不是排队连接的实际数量。
The listen backlog should simply accommodate short term discrepancies between the rate at which the server software can accept new connections and the rate at which these new connections are arriving. You should strive to ensure that your server can accept new connections at a suitable rate, ideally by using AcceptEx()
with IOCP for overlapped accept calls and posting a suitable number of overlapped accepts when your server starts and then topping these up as connections are established.
侦听积压应该简单地适应服务器软件可以接受新连接的速率与这些新连接到达的速率之间的短期差异。您应该努力确保您的服务器能够以合适的速率接受新连接,理想情况下,通过将AcceptEx()
IOCP 用于重叠接受调用并在您的服务器启动时发布适当数量的重叠接受,然后在建立连接时将它们补满。
回答by user2258632
It was 5 back on NTWS 4.0: http://support.microsoft.com/kb/127144
在 NTWS 4.0 上是 5:http: //support.microsoft.com/kb/127144
I believe in XP it was also this way, although from XP on it's hard to find anything concrete from Microsoft. (At least for me, that is how I ended up here hoping for an actual answer).
我相信在 XP 中也是这样,尽管从 XP 开始,很难从 Microsoft 中找到任何具体的东西。(至少对我来说,这就是我最终希望得到实际答案的方式)。
A quick seat-of-the-pants Java test showed that the default for Windows 7 Pro is 50, and the limit is 200. FWIW.
快速上手 Java 测试表明,Windows 7 Pro 的默认值为 50,限制为 200。FWIW。
I'm not sure why someone would answer this with an assumption about whythe question is asked. There are legitimate reasons for knowing the actual answer.
我不确定为什么有人会假设为什么会问这个问题来回答这个问题。知道实际答案是有正当理由的。
For example, you have software where you may receieve a burst of requests. If the max backlog by the operating system is 5, you may end up refusing connections even if handing them off as quickly as possible. And a backlog of 200 alleviates this. And because of this, you may want to make a OS system requirement to not use an OS that has such a backlog limitation.
例如,您的软件可能会收到大量请求。如果操作系统的最大积压为 5,则即使尽快将连接移交,您也可能最终拒绝连接。200 的积压可以缓解这种情况。正因为如此,您可能希望操作系统系统要求不使用具有此类积压限制的操作系统。
回答by Jon
For what it's worth, there is a default of 5 in the async sockets Listen() code for Visual Studio 9.0 (aka 2008).
I found this in c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxsock.h:
BOOL Listen(int nConnectionBacklog=5);
就其价值而言,Visual Studio 9.0(又名 2008)的异步套接字 Listen() 代码中的默认值为 5。我在 c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxsock.h 中找到了这个:
BOOL Listen(int nConnectionBacklog=5);
Also documented here: https://docs.microsoft.com/en-us/cpp/mfc/reference/casyncsocket-class?view=vs-2019#listen
还记录在此处:https: //docs.microsoft.com/en-us/cpp/mfc/reference/casyncsocket-class?view=vs-2019#listen
回答by rjobidon
The backlog parameter of the listen(socket, backlog) function is limited to SOMAXCONN
. If set to SOMAXCONN
, the underlying service provider responsible for the socket will set the backlog to a maximum reasonable value1.
listen(socket, backlog) 函数的 backlog 参数限制为SOMAXCONN
. 如果设置为SOMAXCONN
,则负责套接字的底层服务提供者会将积压设置为最大合理值1。
The maximum value on Windows platforms was2:
Windows 平台上的最大值为2:
- Windows Sockets 1 defines
SOMAXCONN = 5
. Winsock 1.1 was part of Windows 95 and Windows NT 3.5. - Windows Sockets 2 defines
SOMAXCONN = 0x7fffffff
and limits the listen backlog to a large value (typically several hundred or more). Winsock 2.1 was an add-on to Windows 95 and part of Windows 98, Windows NT 4.0 and all subsequent Windows releases. - Windows 8 includes the "RIO" (Registered IO) extensions for Winsock. These extensions are designed to reduce the latency of network applications3. The macro
SOMAXCONN_HINT(N)
can be used to set the backlog to a larger value than possible withSOMAXCONN
. The backlog value will be N, adjusted to be within the range (200, 65535)1.
- Windows 套接字 1 定义了
SOMAXCONN = 5
. Winsock 1.1 是 Windows 95 和 Windows NT 3.5 的一部分。 - Windows Sockets 2 将
SOMAXCONN = 0x7fffffff
侦听积压定义和限制为一个较大的值(通常为数百或更多)。Winsock 2.1 是 Windows 95 的附加组件,是 Windows 98、Windows NT 4.0 和所有后续 Windows 版本的一部分。 - Windows 8 包括 Winsock 的“RIO”(注册 IO)扩展。这些扩展旨在减少网络应用程序3的延迟。该宏
SOMAXCONN_HINT(N)
可用于将积压设置为比SOMAXCONN
. 积压值将为 N,调整为在 (200, 65535) 1范围内。
Note:
注意:
"When calling the listen() function in a Bluetooth application, it is strongly recommended that a much lower value be used for the backlog parameter (typically 2 to 4), since only a few client connections are accepted. This reduces the system resources that are allocated for use by the listening socket. This same recommendation applies to other network applications that expect only a few client connections."1
“在蓝牙应用程序中调用 listen() 函数时,强烈建议为 backlog 参数使用一个低得多的值(通常为 2 到 4),因为只接受少数客户端连接。这减少了系统资源分配给侦听套接字使用。同样的建议适用于其他只需要几个客户端连接的网络应用程序。” 1
Sources:
来源:
回答by Sydius
I've seen it be 200 on Windows 7, but I don't know if it's tied to the OS or if networking drivers etc. might have an influence.
我在 Windows 7 上看到它是 200,但我不知道它是否与操作系统有关,或者网络驱动程序等是否会产生影响。
You can experimentally determine this by listening on a port and making a lot of connections to that port without accepting them until you're done (then see how many you got).
您可以通过侦听端口并与该端口建立大量连接而不接受它们,直到完成(然后查看您获得了多少)来实验性地确定这一点。
If you take Len's advise and use AcceptEx with IOCP, you can pretty much determine for yourself how large of a backlog you have by calling it in advance on a pool of waiting connections. I've seen this work with as many as 1,600 connections in the pool, but it does seem to have a limit as well.
如果您接受 Len 的建议并使用 AcceptEx 和 IOCP,您几乎可以通过在等待连接池上提前调用它来自己确定您有多大的积压。我已经看到池中有多达 1,600 个连接的情况,但它似乎也有限制。