Java HTTP Port Find isHostReachable(String host, int port, int connTimeout)

Here you can find the source of isHostReachable(String host, int port, int connTimeout)

Description

Checks whether the given host and port is reachable or not.

License

Apache License

Parameter

Parameter Description
host a parameter
port a parameter
connTimeout a parameter

Declaration

public static boolean isHostReachable(String host, int port, int connTimeout) 

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 {
    /**// ww  w.j av  a  2 s .  c  om
     * Checks whether the given host and port is reachable or not. Tries to open a
     * socket with the given connection timeout.
     * @param host
     * @param port
     * @param connTimeout
     * @return 
     */
    public static boolean isHostReachable(String host, int port, int connTimeout) {
        Socket socket = null;
        boolean reachable = false;
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(host, port), connTimeout);
            reachable = true;
        } catch (IOException ioe) {
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
        }
        return reachable;
    }
}

Related

  1. isBound(int port)
  2. isConnectionAlive(String protocol, String host, int port)
  3. isFreePort(int portNumber)
  4. isFreeTCPPort(final int port)
  5. isHostAvailable(final String host, final int port)
  6. isListening(String host, int port)
  7. isLivereloadAvailable(int port)
  8. isLocalPortAvailable(final int port)
  9. isLocalPortFree(final int port)