Since Java 1.5 (Tiger) the java.net.InetAddress
class introduces isReachable()
method that can be used to ping or check if the address specified by the InetAddress
is reachable.
package org.kodejava.net;
import java.net.InetAddress;
public class PingExample {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("172.16.2.0");
// Try to reach the specified address within the timeout
// period. If during this period the address cannot be
// reach then the method returns false.
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024