What does the "backlog" argument in the ServerSocket constructor mean?

Tim Rohaly

A server must:

  1. Invoke the accept() method of ServerSocket to listen for a socket connection request
  2. Process the client request or hand off that request to a child process
  3. Repeat
In the space of time between calls to accept(), incoming client connection requests are stored in a queue maintained by the operating system. Subsequent calls to accept() remove requests from this queue, or block if there are no waiting clients. The "backlog" argument controls the length of this queue.

ServerSocket tries to set the backlog to be 50 by default, although some OSes don't support a queue that large or don't allow the queue length to be modified.

When a client requests a connection and the queue is full, the request will fail with a ConnectionException. Increasing this backlog value may prevent some refused connections, but won't help to process these connections any faster and doesn't affect the maximum number of simultaneous connections allowed.