Java InetAddress Ping ping(InetAddress addr, NetworkInterface ni)

Here you can find the source of ping(InetAddress addr, NetworkInterface ni)

Description

ping

License

Apache License

Declaration

public static boolean ping(InetAddress addr, NetworkInterface ni) throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww  w.  j av  a  2s.  co m
 * Copyright (c) 2012-2017 ZoxWeb.com LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.IOException;

import java.net.InetAddress;

import java.net.NetworkInterface;

public class Main {
    public static boolean ping(String host) throws IOException {
        return ping(host, null);
    }

    public static boolean ping(String host, String niName) throws IOException {
        return ping(InetAddress.getByName(host), (niName != null) ? NetworkInterface.getByName(niName) : null);
    }

    public static boolean ping(InetAddress addr, NetworkInterface ni) throws IOException {
        return ping(addr, ni, 255, 5000);
    }

    public static boolean ping(InetAddress addr, NetworkInterface ni, int ttl, int timeout) throws IOException {
        return ping(addr, ni, ttl, timeout, true);
    }

    public static boolean ping(InetAddress addr, NetworkInterface ni, int ttl, int timeout, boolean statOn)
            throws IOException {
        long delta = 0;
        if (statOn) {
            delta = System.currentTimeMillis();
        }
        boolean ret = addr.isReachable(ni, ttl, timeout);

        if (statOn) {
            delta = System.currentTimeMillis() - delta;
            if (ret)
                System.out.println("it took " + delta + " millis to ping: " + addr.getHostName());
            else
                System.out.println("ping timed out after " + timeout + " millis to ping: " + addr.getHostName());

        }

        return ret;
    }
}

Related

  1. ping(final InetAddress address)
  2. ping(InetAddress host)
  3. ping(InetAddress target)
  4. pingFromLocalAddress(InetAddress localAddr)
  5. pingUsingInetAddress()