How to measure response-time of network using Java?

Ashish Bhadiyadra picture Ashish Bhadiyadra · Apr 7, 2009 · Viewed 8.3k times · Source

We have a client and a server. I want to measure the response-time of the network between them. When I send a request to server it should immediate respond to my request, it should be like a ping request so that there will be no processing time at the server.

How can I do this in Java?

Answer

Eddie picture Eddie · Apr 7, 2009

I have done this by sending a packet with a timestamp from client to server, and then having the server return the same timestamp back. When the server receives the timestamp, it can compare against the current timestamp to measure round trip time.

Apparently there is a way to generate an ICMP ping in Java 5 and later. If you want to measure network speeds, just grab System.nanoTime() before and after and compare. Note that if your program is running with insufficient privs to do ICMP ping, then Java will resort to using a TCP echo request to port 7, which will still suit your needs.