Java Host Check isHostReachable(String host, int timeout)

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

Description

Get a value indicating whether the specified host is reachable using the specified timeout.

License

Open Source License

Parameter

Parameter Description
host the host
timeout the timeout in milliseconds used in the network reachability test

Return

a value indicating whether the specified host is reachable using the specified timeout

Declaration

public static boolean isHostReachable(String host, int timeout) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2012 by Min Cai (min.cai.china@gmail.com).
 *
 * This file is part of the PickaPack library.
 *
 * PickaPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.//  w w w .j  a v a2s  .c  o  m
 *
 * PickaPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PickaPack. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.IOException;
import java.net.InetAddress;

public class Main {
    /**
     * Default timeout in milliseconds used in the network reachability test.
     */
    public static final int DEFAULT_TIMEOUT = 400;

    /**
     * Get a value indicating whether the specified host is reachable using the specified timeout.
     *
     * @param host the host
     * @param timeout the timeout in milliseconds used in the network reachability test
     * @return a value indicating whether the specified host is reachable using the specified timeout
     */
    public static boolean isHostReachable(String host, int timeout) {
        try {
            return InetAddress.getByName(host).isReachable(timeout);
        } catch (IOException e) {
            return false;
        }
    }

    /**
     * Get a value indicating whether the specified host is reachable using the default timeout.
     *
     * @param host the host
     * @return a value indicating whether the specified host is reachable using the default timeout
     */
    public static boolean isHostReachable(String host) {
        return isHostReachable(host, DEFAULT_TIMEOUT);
    }
}

Related

  1. checkHost(String host)
  2. checkHost(String hostname)
  3. checkHostIsAvailable(String hostName)
  4. checkHostName(String hostName)
  5. isInternalHostname(String host)
  6. isLocal(final String hostName)
  7. isLocal(final String hostname)
  8. isThisMe(String hostname)