Java HTTP Port Find isReachable(final String hostName, int port, int timeout)

Here you can find the source of isReachable(final String hostName, int port, int timeout)

Description

Checks that given hostname is reacheble in certain timeout.

License

Apache License

Parameter

Parameter Description
hostName Hostname
port Port
timeout Timeout in ms

Return

Reachable or not

Declaration

public static boolean isReachable(final String hostName, int port, int timeout) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.net.InetSocketAddress;

import java.net.Socket;

public class Main {
    /**//from w ww .  ja  v a  2 s .c  o m
     * Checks that given hostname is reacheble in certain timeout.
     * Basically working as {@link java.net.InetAddress#isReachable(int)}, but working on all platforms
     *
     * @param hostName Hostname
     * @param port     Port
     * @param timeout  Timeout in ms
     * @return Reachable or not
     */
    public static boolean isReachable(final String hostName, int port, int timeout) {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(hostName, port), timeout);
            return true;
        } catch (IOException ignore) {
            return false;
        }
    }
}

Related

  1. isPortOpen(String host, int port)
  2. isPortUsed(final int portNumber, final String host)
  3. isPortUsed(int port)
  4. isPortUsing(String host, int port)
  5. isPortUsing(String host, int port)
  6. isReachableByPing(String host, int port)
  7. isServerListening(String host, int port)
  8. isServerListening(String host, int port)
  9. isServerPortFree(int port)