Java socket accept queue length
-
25-09-2019 - |
Question
According to the Sun's documentation on ServerSocket:
The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication arrives when the queue is full, the connection is refused.
How can I increase the the queue length? It's my server's bottle-neck.
Thank you.
Solution
Use the ServerSocket constructor with the backlog
parameter.
You may also want to consider using a Thread pool (or really an ExecutorService) to dispatch incoming requests. Depending on how you architect it, this will generally lead to better throughput.
OTHER TIPS
There is another constructor for ServerSocket.
public ServerSocket(int port, int backlog)
where backlog is the connection queue size you want. The max 50 only applied to the default constructor that takes int port
To limit the connection refused be sure you process all of the connect requests for each select poll before doing any of the send/receive processing for that poll.