title: How can I detect a free port on the server by code from client side?
body: <p>Is there any way by which i can find out free port on the server by executing a code from client computer.</p>
  
  <p>Example for opening a port on my local machine:</p>
  
  <pre><code>ServerSocket localmachine = new ServerSocket(0);
  int port = localmachine.getLocalPort();
  </code></pre>
  
  <p>This way I can retrieve a free port on my local machine.</p>
  
  <p>Now suppose I have my own server: <a href="http://xyz.com" rel="nofollow noreferrer">http://xyz.com</a></p>
  
  <p>How can I get a free port for the server by running a program from client?</p>
  
  <p>Basically I want to initiate a socket connection between client and a server. Which will be further used for communication purpose.</p>