InetAddress addr = InetAddress.getByName("www.google.com"); if (addr.isReachable(5000)) { System.out.println("google.com is reachable"); } else { System.out.println("google.com is not reachable"); }
InetAddress addr = InetAddress.getByName("192.168.1.100"); if (addr.isReachable(5000)) { System.out.println("192.168.1.100 is reachable"); } else { System.out.println("192.168.1.100 is not reachable"); }In this example, we are checking whether the IP address "192.168.1.100" (which is a private IP address) is reachable or not. The isReachable() method is called with a timeout of 5000 milliseconds. If the IP address is reachable, it will print "192.168.1.100 is reachable", otherwise it will print "192.168.1.100 is not reachable". The java.net package library is used in both of these examples to create an instance of the InetAddress class and call its isReachable() method.