Example usage for java.net InetAddress isReachable

List of usage examples for java.net InetAddress isReachable

Introduction

In this page you can find the example usage for java.net InetAddress isReachable.

Prototype

public boolean isReachable(NetworkInterface netif, int ttl, int timeout) throws IOException 

Source Link

Document

Test whether that address is reachable.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress address = InetAddress.getByName("web.mit.edu");
    System.out.println("Name: " + address.getHostName());
    System.out.println("Addr: " + address.getHostAddress());
    System.out.println("Reach: " + address.isReachable(NetworkInterface.getByIndex(0), 0, 3000));
}

From source file:io.joynr.util.JoynrUtil.java

public static void checkConnectivity(String host) throws IOException {
    InetAddress headUnitAddr = InetAddress.getByName(host);
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    NetworkInterface iface;//from   w w w.  j a  v a  2 s.co m

    while (interfaces.hasMoreElements()) {
        iface = interfaces.nextElement();
        if (headUnitAddr.isReachable(iface, 0, 5000)) {
            return;
        }
    }
    throw new UnknownHostException("Connectivity check to the host \"" + host + "\" has failed");
}

From source file:org.blue.star.plugins.check_ping.java

public boolean execute_check() {

    for (String hostname : addresses) {

        try {/*from  www  .  j a  v  a 2 s .c  o m*/

            InetAddress inet = InetAddress.getByName(hostname);

            long execute_time = 0;
            long packet_loss = 0;
            for (int pings = 0; pings < max_packets; pings++) {
                boolean reachable = false;
                long ping_time = System.currentTimeMillis();
                if (network_interface != null) {
                    reachable = inet.isReachable(network_interface, 0, utils_h.timeout_interval);
                } else {
                    reachable = inet.isReachable(utils_h.timeout_interval);
                }
                execute_time += (System.currentTimeMillis() - ping_time);
                if (!reachable)
                    packet_loss++;
            }
            rta = execute_time / max_packets;
            pl = (int) packet_loss / max_packets * 100;

            if (verbose > 0) {
                System.out.println("rta = " + rta);
                System.out.println("pl = " + pl);
            }

            if (verbose > 1) {
                System.out.println("isAnyLocalAddress = " + inet.isAnyLocalAddress());
                System.out.println("isLinkLocalAddress = " + inet.isLinkLocalAddress());
                System.out.println("isLoopbackAddress = " + inet.isLoopbackAddress());
                System.out.println("isMCGlobal = " + inet.isMCGlobal());
                System.out.println("isMCLinkLocal = " + inet.isMCLinkLocal());
                System.out.println("isMCNodeLocal = " + inet.isMCNodeLocal());
                System.out.println("isMCOrgLocal = " + inet.isMCOrgLocal());
                System.out.println("isMCSiteLocal = " + inet.isMCSiteLocal());
                System.out.println("isMulticastAddress = " + inet.isMulticastAddress());
                System.out.println("isSiteLocalAddress = " + inet.isSiteLocalAddress());
                System.out.println("isReachable = " + inet.isReachable(utils_h.timeout_interval));
                System.out.println("getCanonicalHostName = " + inet.getCanonicalHostName());
                System.out.println("getHostAddress = " + inet.getHostAddress());
                System.out.println("getHostName = " + inet.getHostName());
                System.out.println("getClass.getName = " + inet.getClass().getName());
            }

            /* The list is only used to check alternatives, if we pass don't do more */
            if (packet_loss != max_packets)
                break;

        } catch (Exception e) {
            warn_text = e.getMessage();
            e.printStackTrace();
        }

    }

    if (pl >= cpl || rta >= crta || rta < 0)
        check_state = common_h.STATE_CRITICAL;
    else if (pl >= wpl || rta >= wrta)
        check_state = common_h.STATE_WARNING;
    else if (pl >= 0 && rta >= 0)
        check_state = common_h.STATE_OK;

    return true;
}