C++ 如何确定套接字监听()积压参数的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/114874/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:57:11  来源:igfitidea点击:

How to determine the value of socket listen() backlog parameter?

c++csocketstcplisten

提问by Brian R. Bondy

How should I determine what to use for a listening socket's backlog parameter? Is it a problem to simply specify a very large number?

我应该如何确定用于侦听套接字的积压参数的内容?简单地指定一个非常大的数字有问题吗?

采纳答案by Ben Hoffstein

From the docs:

文档

A value for the backlog of SOMAXCONN is a special constant that instructs the underlying service provider responsible for socket s to set the length of the queue of pending connections to a maximum reasonable value.

SOMAXCONN 的 backlog 值是一个特殊的常量,它指示负责 socket 的底层服务提供者将挂起连接队列的长度设置为一个最大的合理值。

回答by Mike Dimmick

There's a very long answer to this in the Winsock Programmer's FAQ. It details the standard setting, and the dynamic backlog feature added in a hotfix to NT 4.0.

Winsock Programmer's FAQ 中有一个很长的答案。它详细说明了标准设置,以及在 NT 4.0 的修补程序中添加的动态积压功能。

回答by smo

I second using SOMAXCONN, unless you have a specific reason to use a short queue.

我第二次使用 SOMAXCONN,除非您有使用短队列的特定原因。

Keep in mind that if there is no room in the queue for a new connection, no RST will be sent, allowing the client to automatically continue trying to connect by retransmitting SYN.

请记住,如果队列中没有空间用于新连接,则不会发送 RST,从而允许客户端通过重新传输 SYN 自动继续尝试连接。

Also, the backlog argument can have different meanings in different socket implementations.

此外,backlog 参数在不同的套接字实现中可能具有不同的含义。

  • In most it means the size of the half-open connection queue, in some it means the size of the completed connection queue.
  • In many implementations, the backlog argument will multiplied to yield a different queue length.
  • If a value is specified that is too large, all implementations will silently truncate the value to maximum queue length anyways.
  • 多数情况下表示半开连接队列的大小,部分表示完成连接队列的大小。
  • 在许多实现中,backlog 参数将相乘以产生不同的队列长度。
  • 如果指定的值太大,则所有实现都会以静默方式将该值截断为最大队列长度。

回答by Brian R. Bondy

As a warning to anyone using boost asio, the SOMAXCONN value is used as 5 with boost.

作为对使用 boost asio 的任何人的警告,SOMAXCONN 值在 boost 中用作 5。