Brian R. Bondy
Brian R. Bondy

Reputation: 347476

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?

Upvotes: 41

Views: 37852

Answers (3)

Mike Dimmick
Mike Dimmick

Reputation: 9802

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.

One of the parameters to the listen() call sets the size of the connection backlog for a particular socket. When the backlog fills up, the stack begins rejecting connection attempts.

The proper value for listen()’s backlog parameter depends on how many connections you expect to see in the time between accept() calls.

The traditional value for listen()’s backlog parameter is 5. This is actually the limit on the home and workstation class versions of Windows.

If your program is quick about calling accept(), low backlog limits are not normally a problem.

You should not take advantage of the feature this way unless your connection rate is very low and the connection times are very short.

Upvotes: 36

smo
smo

Reputation: 913

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.

Upvotes: 4

Ben Hoffstein
Ben Hoffstein

Reputation: 103375

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.

Upvotes: 1

Related Questions