Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get the ping execution time and result in string after ping host

Tags:

java

ping

I want to get the ping execution time and result in string after ping host. How can I do it?

like image 262
Salman Raza Avatar asked Feb 22 '23 21:02

Salman Raza


1 Answers

long currentTime = System.currentTimeMillis();
boolean isPinged = InetAddress.getByName(servername).isReachable(2000); // 2 seconds
currentTime = System.currentTimeMillis() - currentTime;
if(isPinged) {
    System.out.println("pinged successfully in "+ currentTime+ "millisecond");
} else {
    System.out.println("PIng failed.");
}

But this will use ICMP ping only in windows system.

like image 193
Acn Avatar answered May 03 '23 00:05

Acn