public static boolean remotePortTaken ( String node , int port , int timeout ) { Socket s = null ; try { s = new Socket ( ) ; s . setReuseAddress ( true ) ; SocketAddress sa = new InetSocketAddress ( node , port ) ; s . connect ( sa , timeout * 1000 ) ; } catch ( IOException e ) { if ( e . getMessage ( ) . equals ( "Connection refused" ) ) { return false ; } if ( e instanceof SocketTimeoutException || e instanceof UnknownHostException ) { throw e ; } } finally { if ( s != null ) { if ( s . isConnected ( ) ) { return true ; } else { } try { s . close ( ) ; } catch ( IOException e ) { } } return false ; } }
Check if a remote port is taken