title: How long does it take for the OS to close the socket after calling close() method in java?
body: <p>I'm doing some network programming in Java. in a loop I open a socket , send some data and close it. but I get the exception of "connection already in use" . I guess that happens since I use a same port at each repetition of the loop. how long does it take for the OS (Ubuntu 11.10) to close the socket and free the port? thanks</p>
  
  <p>the simplified code look like this</p>
  
  <pre><code>while(true){
              Socket clientSocket = new Socket("localhost", 5000);
              PrintWriter outToServer = new PrintWriter( clientSocket.getOutputStream(),true);    
              outToServer.println ("Hi") ;
              clientSocket.close();   
          }
  </code></pre>