public static void bind ( ServerSocket socket , InetSocketAddress address , int backlog ) throws IOException { try { socket . bind ( address , backlog ) ; } catch ( BindException e ) { BindException bindException = new BindException ( "Problem binding to " + address + " : " + e . getMessage ( ) ) ; bindException . initCause ( e ) ; throw bindException ; } catch ( SocketException e ) { // If they try to bind to a different host's address, give a better // error message. if ( "Unresolved address" . equals ( e . getMessage ( ) ) ) { throw new UnknownHostException ( "Invalid hostname for server: " + address . getHostName ( ) ) ; } else { throw e ; } } }
A convenience method to bind to a given address and report better exceptions if the address is not a valid host .