public static boolean isPortAvailable ( String host , int port ) { try { Socket socket = new Socket ( host , port ) ; socket . close ( ) ; return false ; } catch ( IOException e ) { return true ; } }
See if a port is available for listening on by trying connect to it and seeing if that works or fails