Friday, September 25, 2009

Java - a ping example with java's InetAddress.isReachable()

public class Main {
    public static void main(String[] args) {
            // a note: 
            // This the isReachable method is problematic 
            // If you are in a coorporate environment 
            // then you are probably (unknown to you) 
            // using proxy servers.  Java needs to know 
            // what the proxy server and port is 
            // -DhttpProxy.port 
            // All of testing I've done at home works fine. 
            // You need to configure java to utilize your proxy 
            // gather the ip address associated with the host 
            InetAddress[] addresses = InetAddress.getAllByName("");
            // iterate through the ip address with for-each 
            for (InetAddress addr:addresses) {
                // the timeout is in milliseconds 2 seconds here 
                if (addr.isReachable(2000)){
                    System.out.printf("%s is reachable", addr);
                    System.out.printf("%s is not reachable", addr);
        catch(Exception e) {
            System.out.println("host is unknown (or unresolvable)");
 *      Output (for me): is reachable is reachable is reachable 

No comments:

Post a Comment