Java InetAddress Ping ping(InetAddress target)

Here you can find the source of ping(InetAddress target)

Description

Get the answer time of the host

License

Open Source License

Parameter

Parameter Description
target The host to ping to

Return

The answer time in milliseconds, or -1 if the timeout gets reached

Declaration

public static int ping(InetAddress target) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  w  ww .  j a v a2 s.  c om*/
     * Get the answer time of the host
     * @param target The host to ping to
     * @return The answer time in milliseconds, or -1 if the timeout gets reached
     */
    public static int ping(InetAddress target) {
        return ping(target, 300);
    }

    /**
     * Get the answer time of the host
     * @param target The host to ping to
     * @param timeout Time after the ping should be seen as lost
     * @return The answer time in milliseconds, or -1 if the timeout gets reached
     */
    public static int ping(InetAddress target, int timeout) {
        long ping = -1;
        try {
            long start = System.currentTimeMillis();
            InetAddress.getByName("google.de").isReachable(timeout);
            ping = System.currentTimeMillis() - start;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) ping;
    }
}

Related

  1. ping(InetAddress addr, NetworkInterface ni)
  2. ping(InetAddress host)
  3. pingFromLocalAddress(InetAddress localAddr)
  4. pingUsingInetAddress()
  5. pingWindows(InetAddress address, long timeout)
  6. ping(final InetAddress address)