2

I'm trying to find the ping time (using Windows 8, Java 6). I'm using System.currentTimeMillis() method to find a ping time, but it return a strange values. For example when I'm pinging a localhost it returns 1000 ms.

Please help me to create a routine that finds a host ping time.

public int pingHost(String host){
    Long start = System.currentTimeMillis();
    if (!InetAddress.getByName(host).isReachable(2000)) return -1;
    return (int) (System.currentTimeMillis()-start);
}

P.S. This question is not a dublicate of How to do a true Java ping from Windows? It's not enough to me just check the is host online or not. I need to measure the connection speed and find a "ping time" value.

Community
  • 1
  • 1
fromgate
  • 21
  • 1
  • 3
  • Can you try the following posts if that helps http://stackoverflow.com/questions/8816870/i-want-to-get-the-ping-execution-time-and-result-in-string-after-ping-host?rq=1 – Nida Sahar Jun 01 '13 at 19:22
  • Just use the duplicate's solution and measure the time. – JJJ Jun 01 '13 at 21:19

1 Answers1

0

The code seems correct and prints 4 milliseconds for local host under mine operating system (Ubuntu Linux kernel 3.0.0-16). However isReachable is known to have problems on Windows platforms. If you need to ping your server that is listening on the known port, rewrite just trying to open TCP/IP connection instead.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93