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(int timeout) throws IOException 

Source Link

Document

Test whether that address is reachable.

Usage

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    try {//from w  w  w . jav  a2 s. c o  m

        int timeout = 2000;
        InetAddress[] addresses = InetAddress.getAllByName("www.java2s.com");
        for (InetAddress address : addresses) {
            if (address.isReachable(timeout))
                System.out.printf("%s is reachable%n", address);
            else
                System.out.printf("%s could not be contacted%n", address);
        }
    } catch (Exception e) {
        System.out.printf("Unknown host: %s%n", "www.java2s.com");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getByName("17.16.15.14");

    boolean reachable = address.isReachable(10000);

    System.out.println("Is host reachable? " + reachable);
}

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(3000));
}

From source file:Main.java

public static boolean pingIP(String ip) {
    boolean isReachable = false;
    try {/*from   w w w .  ja va  2  s .  co m*/
        InetAddress address = InetAddress.getByName(ip);
        isReachable = address.isReachable(3000);
    } catch (Exception e) {
    }
    return isReachable;
}

From source file:com.eviware.soapui.impl.support.HttpUtils.java

public static boolean ping(String host, int timeout) {
    pingErrorMessage = "No Error";
    try {/*from  w  w w  .  j  av a 2 s  . c om*/
        InetAddress address = InetAddress.getByName(host);
        return address.isReachable(timeout);
    } catch (Exception e) {
        pingErrorMessage = e.getMessage();
        return false;
    }
}

From source file:com.madgag.agit.GitTestUtils.java

public static String gitServerHostAddress() throws IOException, FileNotFoundException, UnknownHostException {
    File bang = new File(Environment.getExternalStorageDirectory(), "agit-integration-test.properties");
    Properties properties = new Properties();
    if (bang.exists()) {
        properties.load(new FileReader(bang));
    }/*from   w  ww . j  av  a2  s  .  c om*/
    String hostAddress = properties.getProperty("gitserver.host.address", "10.0.2.2");
    InetAddress address = InetAddress.getByName(hostAddress);
    assertThat("Test gitserver host " + hostAddress + " is reachable", address.isReachable(1000), is(true));
    return hostAddress;
}

From source file:Main.java

public static void printAddressDetails(String host) throws Exception {
    System.out.println("Host '" + host + "' details starts...");
    InetAddress addr = InetAddress.getByName(host);
    System.out.println("Host  IP  Address: " + addr.getHostAddress());
    System.out.println("Canonical  Host  Name: " + addr.getCanonicalHostName());

    int timeOutinMillis = 10000;
    System.out.println("isReachable(): " + addr.isReachable(timeOutinMillis));
    System.out.println("isLoopbackAddress(): " + addr.isLoopbackAddress());

}

From source file:org.encuestame.core.util.InternetUtils.java

/**
 * Ping hostname.//w  ww. j  ava 2s  . co m
 * @param domain
 * @return
 */
public static boolean hostReachable(final String domain) {
    InetAddress host;
    boolean reachable = false;
    try {
        // Try to reach the specified address within the timeout
        // periode. If during this periode the address cannot be
        // reach then the method returns false.
        host = InetAddress.getByName(domain);
        reachable = host.isReachable(InternetUtils.TIMEOUT);
    } catch (UnknownHostException e) {
        log.error("hostReachable " + e.getMessage());
    } catch (IOException e) {
        log.error("hostReachable " + e.getMessage());
    }
    return reachable;
}

From source file:org.cloudifysource.dsl.utils.IPUtils.java

/**
 * Resolves the host name and returns its IP address.
 * //from  ww w .j a va  2  s.  c  o m
 * @param hostName
 *            The name of the host
 * @return The IP address of the host
 * @throws UnknownHostException
 *             Indicates the host doesn't represent an available network
 *             object
 */
public static String resolveHostNameToIp(final String hostName) throws UnknownHostException {
    final InetAddress byName = InetAddress.getByName(hostName);
    try {
        if (byName.isReachable(DEFAULT_CONNECTION_TIMEOUT * MILLISECONDS_IN_A_SECOND)) {
            return byName.getHostAddress();
        } else {
            return null;
        }
    } catch (final IOException e) {
        return null; // not reachable
    }
}

From source file:com.flexive.shared.stream.FxStreamUtils.java

/**
 * Probe all network interfaces and return the most suited to run a StreamServer on.
 * Preferred are interfaces that are not site local.
 *
 * @return best suited host to run a StreamServer
 * @throws UnknownHostException on errors
 *//*from   w  w  w.  j  a  v a2s. co m*/
public static InetAddress probeNetworkInterfaces() throws UnknownHostException {
    try {
        final String forcedAddress = System.getProperty("FxStreamIP");
        if (forcedAddress != null) {
            try {
                InetAddress ad = InetAddress.getByName(forcedAddress);
                LOG.info("Binding [fleXive] streamserver to forced address [" + forcedAddress + "] ...");
                return ad;
            } catch (UnknownHostException e) {
                LOG.error("Forced [fleXive] streamserver bind address [" + forcedAddress + "] can not be used: "
                        + e.getMessage() + " - probing available network interfaces ...");
            }
        }
        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
        NetworkInterface nif;
        InetAddress preferred = null, fallback = null;
        while (nifs.hasMoreElements()) {
            nif = nifs.nextElement();
            if (LOG.isDebugEnabled())
                LOG.debug("Probing " + nif.getDisplayName() + " ...");
            if (nif.getDisplayName().startsWith("vmnet") || nif.getDisplayName().startsWith("vnet"))
                continue;
            Enumeration<InetAddress> inas = nif.getInetAddresses();
            while (inas.hasMoreElements()) {
                InetAddress na = inas.nextElement();
                if (LOG.isDebugEnabled())
                    LOG.debug("Probing " + nif.getDisplayName() + na);
                if (!(na instanceof Inet4Address))
                    continue;
                if (!na.isLoopbackAddress() && na.isReachable(1000)) {
                    if (preferred == null || (preferred.isSiteLocalAddress() && !na.isSiteLocalAddress()))
                        preferred = na;
                }
                if (fallback == null && na.isLoopbackAddress())
                    fallback = na;
            }
        }
        if (LOG.isDebugEnabled())
            LOG.debug("preferred: " + preferred + " fallback: " + fallback);
        if (preferred != null)
            return preferred;
        if (fallback != null)
            return fallback;
        return InetAddress.getLocalHost();
    } catch (Exception e) {
        return InetAddress.getLocalHost();
    }
}