forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsiteping.java
More file actions
22 lines (19 loc) · 780 Bytes
/
websiteping.java
File metadata and controls
22 lines (19 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.net.InetAddress;
public class PingExample {
public static void main(String[] args) {
String host = "www.google.com";
try {
InetAddress inetAddress = InetAddress.getByName(host);
long startTime = System.currentTimeMillis();
if (inetAddress.isReachable(5000)) {
long endTime = System.currentTimeMillis();
long pingTime = endTime - startTime;
System.out.println("Ping for " + host + " Successful. Time: " + pingTime + "ms");
} else {
System.out.println("Unable to ping host " + host);
}
} catch (Exception e) {
System.out.println("Error pinging host " + host + ": " + e.getMessage());
}
}
}