Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 853
Brian R. Bondy Avatar asked Sep 22 '08 13:09

Brian R. Bondy


People also ask

What is backlog in socket listen?

The backlog has an effect on the maximum rate at which a server can accept new TCP connections on a socket. The rate is a function of both the backlog value and the time that connections stay on the queue of partially open connections.

What is the purpose of listen () method in socket programming?

The listen() call indicates a readiness to accept client connection requests. It transforms an active socket into a passive socket. Once called, socket can never be used as an active socket to initiate connection requests. Calling listen() is the third of four steps that a server performs to accept a connection.

What is the second parameter of the listen () function for?

The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. The second argument to this function specifies the maximum number of connections the kernel should queue for this socket.

What is Somaxconn?

SOMAXCONN defines the maximum number you're allowed to pass to listen() which is 128 on my system.


2 Answers

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.

like image 184
Mike Dimmick Avatar answered Sep 20 '22 18:09

Mike Dimmick


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.
like image 32
smo Avatar answered Sep 19 '22 18:09

smo