Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

booleanisBound(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) {
...
booleanisConnectionAlive(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;
booleanisFreePort(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;
booleanisFreeTCPPort(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;
...
booleanisHostAvailable(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 {
...
booleanisHostReachable(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 {
...
booleanisListening(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) {
...
booleanisLivereloadAvailable(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) {
...
booleanisLocalPortAvailable(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;
booleanisLocalPortFree(final int port)
is Local Port Free
try {
    new ServerSocket(port).close();
    return true;
} catch (final IOException e) {
    return false;