private static boolean isPortOpen ( InetAddress pAddress , int pPort ) { Socket socket = null ; try { socket = new Socket ( ) ; socket . setReuseAddress ( true ) ; SocketAddress sa = new InetSocketAddress ( pAddress , pPort ) ; socket . connect ( sa , 200 ) ; return socket . isConnected ( ) ; } catch ( IOException e ) { return false ; } finally { if ( socket != null ) { try { socket . close ( ) ; } catch ( IOException e ) { // Best effort. Hate that close throws an IOException, btw. Never saw a real use case for that. } } } }
Check a port by connecting to it . Try only 200ms .