socket listen backlog parameter, how to determine

2019-01-17 14:27发布

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?

标签: c++ tcp sockets
4条回答
来,给爷笑一个
2楼-- · 2019-01-17 14:46

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.

查看更多
走好不送
3楼-- · 2019-01-17 14:51

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

查看更多
Luminary・发光体
4楼-- · 2019-01-17 14:55

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-17 14:56

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

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.

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

  • 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.
查看更多
登录 后发表回答