Java socket accept queue length
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.
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.
Use the backlog
parameter of the constructor (Javadoc). Keep in mind that you won't be able to increase the queue past the operating system limits, though. This is to prevent SYN attacks - see this article for more information.
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.
精彩评论