views:

1626

answers:

3

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?

+1  A: 

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.

Eddie
But i dont have access to server,i can change only client application.
Ashish Bhadiyadra
If you don't have access to the server, is there anything in the server that you can use to return something to the client that it can track?
Eddie
i dont have any access to the server as well as every time server address will be different.so we have only the client to which we can interact and can modify.
Ashish Bhadiyadra
What protocol is being used?
Eddie
i think java support only UDP and TCP.but in ping request ICMP is used,then how can we implement that in java?
Ashish Bhadiyadra
See http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/
Eddie
+1  A: 

To ensure that the server response as fast as possible, you should either

  1. have a second connection to a different port - this is useful if you want to test the route to the server, i.e. the wires - or
  2. a common thread for all incoming packets, that will decide whether an incoming packet is a ping request and respond immediately - which is useful if you want to test the current connection (which takes other packets on that connection into account).

Either way the listening part (server) should have its own Thread or the processing of the request could stale while other packets are being processed.

Note that, whatever you do to measure the speed of the connection, you will not have a reliable value. You should have an acceptable value, though, if you take the average of several such pings.

soulmerge
+1  A: 

I think you need to distinguish between the time taken to the server (machine), and the time for your server-side process to handle this. For example, ping will simply measure the ICMP response time, which is a low-level network protocol return. If you take into account the TCP stack, and then the subsequent processing by the server, that time period will be greater.

From your question, it sounds like ping (ICMP) is what you want. In JDK 5 and above, you can do:

String host = "192.168.0.1"
int timeOut = 5000; 
boolean status = InetAddress.getByName(host).isReachable(timeOut)

and you would need to time that using System.nano() or similar.

Brian Agnew
Thank you for your help.But how can we measure responsetime using this method as it is returning only boolean value.kinaly help.
Ashish Bhadiyadra
Get the number of nanoseconds before you call it. Then call it. Then get the number of nanoseconds afterwards. Subtract one from the other, and you've got the time period.
Brian Agnew
Ya i had done the same,but its not matching with the ping request.In ping request i got 5 miliseconds while i got 1024 milisecond for the same IP address with our code.I think its not reliable.If you can help then i will be greatful.
Ashish Bhadiyadra
A quick test on my system looks good. 2 questions. 1) 1,000,000ns = 1ms, yes ? 2) because of Java's classloading and optimisation whilst running, you may want to do this 'n' times in a loop and take the average
Brian Agnew
Yes,1,000,000ns = 1ms. You can just try another ip address ,not your system ip.And if you have solution for this ,pls let me know.
Ashish Bhadiyadra
Actually, due to warm up time, you want to ignore the timing of the first few attempts.
Peter Lawrey