-
Notifications
You must be signed in to change notification settings - Fork 0
/
PingExample.java
39 lines (37 loc) · 1.04 KB
/
PingExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.net.InetAddress;
import java.net.UnknownHostException;
public class PingExample
{
private String hostName;
//Constructor to set the hostName for the ping
public PingExample(String hostName)
{
this.hostName = hostName;
}
//Method to perform a network ping
public void performPing()
{
try
{
InetAddress inetAddress = InetAddress.getByName(hostName);
if (inetAddress.isReachable(5000))
{
System.out.println(hostName + " is reachable");
}
else
{
System.out.println(hostName + " is not reachable");
}
}
catch (UnknownHostException e)
{
//Handles the unknown host exception
System.err.println("Unknown host: " + hostName);
}
catch (Exception e)
{
//Handles the general exception during ping
System.err.println("Error while pinging " + hostName + ": " + e.getMessage());
}
}
}