40

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?

2
  • As a warning to anyone using boost asio, the SOMAXCONN value is used as 5 with boost. Sep 22, 2008 at 14:49
  • I don't think Boost Asio defines this value per se. It has it's own constant, socket_base::max_listen_connections that is assigned the system SOMAXCONN value.
    – selbie
    May 29, 2020 at 9:05

3 Answers 3

36

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.

2
  • 2
    Fantastic answer in that FAQ; thanks for sharing. Recommended. Mar 27, 2012 at 17:50
  • 1
    If you're on a Linux box, see the listen manpage: "If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128."
    – tonysdg
    May 15, 2018 at 14:52
4

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.
1

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
  • 50
    Terrible answer. What are such "reasonable values"?
    – Nyerguds
    Apr 2, 2013 at 14:18
  • 7
    I have to agree, this doesn't answer the question at all. Jun 5, 2013 at 11:27
  • 5
    I disagree with the other comments. This is a perfectly good answer to the common question: "What the heck should I pass as the second parameter to listen()?" If you don't know, use SOMAXCONN. Jun 30, 2016 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.