List of utility methods to do HTTP Port Find
boolean | isBound(int port) is Bound Socket socket = null; try { socket = new Socket("localhost", port); socket.setReuseAddress(true); return true; } catch (UnknownHostException e) { throw new RuntimeException(e); } catch (ConnectException ignored) { ... |
boolean | isConnectionAlive(String protocol, String host, int port) is Connection Alive boolean isAlive = true; try { URL url = new URL(protocol, host, port, ""); URLConnection connection = url.openConnection(); connection.connect(); } catch (Exception e) { isAlive = false; return isAlive; |
boolean | isFreePort(int portNumber) Checks whether the given TCP port is available. try { ServerSocket socket = new ServerSocket(portNumber); socket.setReuseAddress(true); socket.close(); return true; } catch (IOException e) { return false; |
boolean | isFreeTCPPort(final int port) is Free TCP Port try { final ServerSocket ss = new ServerSocket(port); ss.setReuseAddress(true); ss.close(); return true; } catch (final Throwable t) { t.printStackTrace(); return false; ... |
boolean | isHostAvailable(final String host, final int port) Returns whether the given host is accepting connections on the given port. assert host != null : "the host must not be null"; assert !host.isEmpty() : "the host must not be empty"; assert port >= 0 && port <= UPPER_PORT_BOUND : "the port be within the range 0 - 65535"; Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(host, port), CONNECTION_TIMEOUT); } catch (IOException ex) { try { ... |
boolean | isHostReachable(String host, int port, int connTimeout) Checks whether the given host and port is reachable or not. Socket socket = null; boolean reachable = false; try { socket = new Socket(); socket.connect(new InetSocketAddress(host, port), connTimeout); reachable = true; } catch (IOException ioe) { } finally { ... |
boolean | isListening(String host, int port) is Listening Socket socket = null; try { socket = new Socket(host, port); return socket.isConnected(); } catch (ConnectException e) { return false; } finally { if (socket != null) { ... |
boolean | isLivereloadAvailable(int port) is Livereload Available try { HttpURLConnection.setFollowRedirects(false); URL url = new URL("http://localhost:" + port + "/livereload.js"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(1000); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (IOException e) { ... |
boolean | isLocalPortAvailable(final int port) Checks to see if a specific local port is available. try (final DatagramSocket ds = new DatagramSocket(port); final ServerSocket ss = new ServerSocket(port)) { ss.setReuseAddress(true); ds.setReuseAddress(true); return true; } catch (final IOException ignored) { return false; |
boolean | isLocalPortFree(final int port) is Local Port Free try { new ServerSocket(port).close(); return true; } catch (final IOException e) { return false; |