isReachable(int timeout) method of InetAddress class test whether that address is reachable.
|
Test if a IP address is reachable |
package javamynetwork;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @web http://java-buddy.blogspot.com/
*/
public class JavaMyNetwork {
static String targetIP1 = "192.168.111.107";
static String targetIP2 = "192.168.111.108";
public static void main(String[] args) {
String testIp = "192.168.111.107";
System.out.println(testIp + " is reachable: "
+ isIpReachable(testIp));
testIp = "192.168.111.108";
System.out.println(testIp + " is reachable: "
+ isIpReachable(testIp));
}
private static boolean isIpReachable(String targetIp) {
boolean result = false;
try {
InetAddress target = InetAddress.getByName(targetIp);
result = target.isReachable(5000); //timeout 5sec
} catch (UnknownHostException ex) {
System.out.println(ex.toString());
} catch (IOException ex) {
System.out.println(ex.toString());
}
return result;
}
}
No comments:
Post a Comment