Reputation: 923
In my Java application I need to get the ping of multiple connections at once, I have tried using a thread for each connection and measuring the response time but they don't all seem to be correct.
Is there a thread safe way to measure the ping/response time of a connection? I am currently using:
Thread thread = new Thread(new Runnable() {
public void run(){
long start = System.currentTimeMillis();
someInetAddress.isReachable(5000);
long timeTaken = System.currentTimeMillis() - start;
}
});
However timeTaken doesn't seem to be correct as users with 150 ping are getting 5000 (probably more because it seems to have timed out)
Help is appreciated! Keir
Edit: Okay found out that its due to Port 7 being closed for some people, is there any other way to ping them without them having to have Port 7 closed?
Upvotes: 0
Views: 876
Reputation: 311039
The isReachable()
method works by trying to connect to TCP port 7 (echo). It doens't actually care whether port 7 is closed or not. All it cares about is whether there is a response of any kind to the connect. A ConnectException
rates as isReachable = true
, and it should take about the same amount of time as a successful connection, maybe even quicker. A connect timeout, host not reachable, etc, rate as false
.
The method is advertised to use ICMP in very limited circumstances: you aren't on Windows and you have root privilege. In practice this never applies.
Upvotes: 1