Ping a server jdk1.5From WikiJava
Since JDK1.5, java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not. This example shows how to perform a pseudo ping in Java
the articleisReachable() will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host. But most Internet sites have disabled the service or blocked the requests (except some university web sites like the example above) . ReachableTest.javaimport java.io.*; import java.net.*; public class ReachableTest { public static void main(String args[]) { try { InetAddress address = InetAddress.getByName("web.mit.edu"); System.out.println("Name: " + address.getHostName()); System.out.println("Addr: " + address.getHostAddress()); System.out.println("Reach: " + address.isReachable(3000)); } catch (UnknownHostException e) { System.err.println("Unable to lookup web.mit.edu"); } catch (IOException e) { System.err.println("Unable to reach web.mit.edu"); } } } See AlsoPing a server, Real's Java How To |
