Here you can find the source of getPing(InetAddress address, long timeout)
public static long getPing(InetAddress address, long timeout)
//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); } }