Java InetAddress Create getPing(InetAddress address, long timeout)

Here you can find the source of getPing(InetAddress address, long timeout)

Description

get Ping

License

Apache License

Declaration

public static long getPing(InetAddress address, long timeout) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.net.*;

public class Main {
    public static long getPing(InetAddress address, long timeout) {
        if (System.getProperty("os.name").startsWith("Windows")) {
            return pingWindows(address, timeout);
        }/*from w  ww  .jav  a  2s .  c om*/

        boolean reached = false;
        long start = System.currentTimeMillis();
        long end = (start - 1);

        try {
            if (address.isReachable(Long.valueOf(timeout).intValue())) {
                end = System.currentTimeMillis();
            }
        } catch (IOException ex) {
            if (reached) {
                return calculatePingDelay(1, 0);
            }
        }

        return calculatePingDelay(start, end);
    }

    private static long pingWindows(InetAddress address, long timeout) {
        boolean reached = false;
        long start = System.currentTimeMillis();
        long end = (start - 1);

        try {
            String line;
            ProcessBuilder processBuilder = new ProcessBuilder(
                    new String[] { "ping", address.getHostAddress(), "-w", String.valueOf(timeout), "-n", "1" });
            Process process = processBuilder.start();

            int processing = 0;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {
                processing++;
                if (processing == 2) {
                    start = System.currentTimeMillis();
                }

                else if (processing == 3) {
                    if (line.contains(address.getHostAddress())) {
                        end = System.currentTimeMillis();
                    }
                }
            }

            if (calculatePingDelay(start, end) > timeout) {
                return calculatePingDelay(1, 0);
            }

            bufferedReader.close();
        } catch (IOException ex) {
            if (reached) {
                return calculatePingDelay(1, 0);
            }
        }

        return calculatePingDelay(start, end);
    }

    private static long calculatePingDelay(long start, long end) {
        return (end - start);
    }
}

Related

  1. getNetworkInterfaceByIp(InetAddress inetAddress)
  2. getNetworkInterfaceForRemoteIPAddress( InetAddress remoteAddress)
  3. getNextAliablePort(InetAddress address, int startport)
  4. getNextAvailablePort(InetAddress address, int port)
  5. getOneDotPrefix(InetAddress addr)
  6. getPrivateInetInetAddress()
  7. getProtocolFamily(InetAddress address)
  8. getSafeInetAddress(String name, JSONObject json)
  9. getSourceAddressForDestination(InetAddress destination)