private ServerSocket createServerSocket ( int port ) throws IOException { try { ServerSocket sock = new ServerSocket ( ) ; // Prevent 2MSL delay problem on server restarts sock . setReuseAddress ( true ) ; // Bind to listening port if ( port == 0 ) { sock . bind ( null ) ; serverPort = sock . getLocalPort ( ) ; } else { sock . bind ( new InetSocketAddress ( port ) ) ; } return sock ; } catch ( IOException ioe ) { throw new IOException ( "Could not create ServerSocket on port " + port + "." + ioe ) ; } }
Bind to port . If the specified port is 0 then bind to random port .