Ping IP Address In Java Example
Chapter:
Networking
Last Updated:
21-09-2018 15:37:53 UTC
Program:
/* ............... START ............... */
import java.net.*;
public class JavaPingExample {
public static void main(String[] args) {
try {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
if (inet.isReachable(5000)) {
System.out.println(ipAddress + " is reachable.");
} else {
System.out.println(ipAddress + " NOT reachable.");
}
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
}
}
}
/* ............... END ............... */
Output
Sending Ping Request to 127.0.0.1
127.0.0.1 is reachable.
Notes:
-
The Ping command allows you to test the connection speed between you and another network node. You can use it to tell the strength, distance, and availability of a connection, either in your own network or over the internet.
- Syntax : public static InetAddress getByName(String host) throws UnknownHostException.
- In the program InetAddress.getByName(ipAddress) returns the instance of InetAddress containing LocalHost IP and name.
- inet.isReachable is used to ping IP Address.
Tags
Java Ping IP Address , java ping library, java 8 ping, ping program in java source code, java socket ping, java ping url