Search code examples
javadelaypinghostroundtrip

A way to calculate the round trip time in Java with an IP address, beside the ping command


I've implemented a way to get the RTT in Java on unix systems with the following code:

String command[] = {"ping", "-c4", theIPAddress};
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = in.readLine()) != null)
   //the terminal output with the ping results...
etc...

This works fine and I can calculate the RTT (delay) in ms with the output. However, if a host has blocked ICMP, the ping will time-out. (Even in the Mac OS X Terminal or Windows CMD.)

Now I want to find an alternative way to somehow get the RTT/delay for an IP-address. (I don't want to install software on the host side.) If you can't provide me a Java example, please give me other input. I can translate between many languages, and I really can use Google.


Solution

  • Tracert is short for treaceroute (and is windows command last I checked), what is just path ping - it also uses ICMP.

    On linux the equivalent command is tracepath and on Unix-like operating systems, the traceroute utility by default uses User Datagram Protocol (UDP) datagrams with destination port numbers from 33434 to 33534. The traceroute utility usually has an option to specify use of ICMP echo request (type 8) instead, as used by the Windows tracert utility.

    There are also traceroute implementations that use TCP packets, such as tcptraceroute or layer four traceroute.

    Source: traceroute