How to avoid a NoRouteToHostException?
Disclosure: the code I'm working on is for university coursework.
Background: The task I'm trying to complete is to report on the effect of different threading techniques. To do this I have written several classes which respond to a request from a client using Java Sockets. The idea is to flood the server with requests and report on how different threading strategies cope with this. Each client will make 100 requests, and in each iteration we're increasing the number of clients by 50 until something breaks.
Problem: repeatably, and consistently, an exception occurs:
Caused by: java.net.NoRouteToHostException: Cannot assign requested address at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
This happens in several scenarios, including when both the client and server are running on localhost. Connections can be made successfully for a while, it's soon after trying to connect 150 clients that the exception is thrown.
My first thought was that it could be Linux's limit on open file descriptors (1024) but I don't think so. I also checked that any and all connections between the sockets are closed properly (i.e. within a correct block).finally
I'm hesitant to post the code because I'm not sure which parts would be the most relevant, and don't want to have a huge listing of code in the question.
Has anyone come across this before? How can I avoid the NoRouteToHostException?
EDIT (further questions are italicised)
Some good answers so far which point to either the The Ephemeral Port Range or RFC 2780. Both of which would suggest that I have too many connections open. For both it appears the number of connections which need to be made to reach this limit suggest that at some point I'm not closing connections.
Having debugged both client and server, both have been observed to hit the method call . This would suggest that connections are being closed (at least in the non-exceptional case). Is this a correct suggestion?myJava-Net-SocketInstance.close()
Also, is there an OS level wait required for ports to become available again? It would be a possibility to run the program a separate time for each 50+ clients if it would just require a short period (or optimistically, running a command) before running the next attempt.
EDIT v2.0
Having taken the good answers provided, I modified my code to use the method setReuseAddress(true) with every Socket connection made on the client. This did not have the desired effect, and I am still limited to 250-300 clients. After the program terminates, running the command shows that there is a lot of socket connections in the TIME_WAIT status. netstat -a
My assumption was that if a socket was in the status, and had been set with the option, any new sockets attempting to use that port would be able to - however, I am still receiving the NoRouteToHostException.TIME-WAIT
SO-REUSEADDR
Is this correct? Is there anything else which can be done to solve this problem?