Disclosure: The code I'm working on is a term paper at a university.
Reference Information. The task I am trying to complete is to report on the impact of various stream processing methods. To do this, I wrote several classes that respond to a request from a client using Java Sockets. The idea is to load the server with requests and report how various thread strategies handle this. Each client will make 100 requests, and at each iteration we increase the number of clients by 50 until something breaks.
Problem: An exception repeatedly and consistently occurs:
Caused by: java.net.NoRouteToHostException: Cannot assign requested address
at java.net.PlainSocketImpl.socketConnect (Native Method)
at java.net.PlainSocketImpl.doConnect (PlainSocketImpl.javahaps33)
This occurs in several scenarios, including when the client and server are running on the local host. Connections may be successful for a while, and shortly after trying to connect 150 clients that throw an exception.
My first thought was that this is a Linux limitation for open file descriptors (1024), but I don't think so. I also verified that all connections between sockets are closed properly (i.e. in the correct finally block).
I hesitate to publish the code, because Iβm not sure which parts will be most relevant and donβt want to have a huge list of code in the question.
Has anyone come across this before? How can I avoid a NoRouteToHostException?
EDIT (additional questions in italics)
There are still good answers that point to either the Ephemeral Port Range or RFC 2780. Both of them suggest that I have too many connections. For both, he shows that the number of connections that must be made to reach this limit suggests that at some point I am not closing the connections.
After debugging both the client and the server, both watched how the call to the myJava-Net-SocketInstance.close() method hit. This assumes that the connections are closed (at least in the non-exclusive case). Is this the right offer?
In addition, is there an OS level needed to resume access to ports? It would be possible to run the program separately for each 50+ clients, if the next attempt just requires a short period (or optimistic, running the command).
EDIT v2.0
Having received good answers, I changed my code to use the setReuseAddress (true) method with every socket connection made on the client. This did not have the desired effect, and I am still limited to 250-300 clients. When the program finishes executing the netstat -a command, there are many socket connections in the TIME_WAIT status.
My assumption was that if the socket was in TIME-WAIT state and was installed with the SO-REUSEADDR , any new sockets trying to use this port might - however, I still get a NoRouteToHostException.
It is right? Is there anything else that can be done to solve this problem?