title: How to avoid a NoRouteToHostException?
body: <p>Disclosure: the code I'm working on is for university coursework.</p>
  
  <p>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.</p>
  
  <p>Problem: repeatably, and consistently, an exception occurs:</p>
  
  <pre>
  Caused by: java.net.NoRouteToHostException: Cannot assign requested address
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
  </pre>
  
  <p>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.</p>
  
  <p>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 <code>finally</code> block).</p>
  
  <p>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.</p>
  
  <p>Has anyone come across this before? How can I avoid the NoRouteToHostException?</p>
  
  <hr>
  
  <p>EDIT (further questions are italicised)</p>
  
  <p>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.</p>
  
  <p>Having debugged both client and server, both have been observed to hit the method call <code>myJava-Net-SocketInstance.close()</code>. This would suggest that connections are being closed (at least in the non-exceptional case). <em>Is this a correct suggestion?</em></p>
  
  <p>Also, <em>is there an OS level wait required for ports to become available again?</em> 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.</p>
  
  <hr>
  
  <p>EDIT v2.0</p>
  
  <p>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 <code>netstat -a</code> shows that there is a lot of socket connections in the TIME_WAIT status. </p>
  
  <p>My assumption was that if a socket was in the <code>TIME-WAIT</code> status, and had been set with the <code>SO-REUSEADDR</code> option, any new sockets attempting to use that port would be able to - however, I am still receiving the NoRouteToHostException.</p>
  
  <p><em>Is this correct?</em> 
  <em>Is there anything else which can be done to solve this problem?</em></p>