strauberry
strauberry

Reputation: 4199

How to test if a remote system is reachable

I want to test whether a remote system is reachable using Java or in other words "send a ping" using Java. This functionality should be encapsulated in a method with boolean value, for example

public boolean isReachable(String ip) {
   // what to do :-)
}

I've tested the Java Process class, but I don't think that it is the best way to do this, because of the complex output handling with OutputBuffers.

Process proc = Runtime.getRuntime().exec("ping " + ip);

Another possibility would be creating a Socket Connection and handle thrown exceptions, but if the remote system is a "naked" unix system, there might be no Socket on the other side :-) Additionally, I'd like to be able to set a timeout, when a remote system is not reachable.

So how could I do this? Thank you!

Upvotes: 24

Views: 40345

Answers (5)

sedrakpc
sedrakpc

Reputation: 564

From my experience there is no 100% reliable way to do this you have to chose by trying multiple options or combining them, but isReachable() can't be used as reliable option. Better pure java option I think will be something like this:

private static boolean isReachable(String host, int openPort, int timeOutMillis) {
    try {
        try (Socket soc = new Socket()) {
            soc.connect(new InetSocketAddress(host, openPort), timeOutMillis);
        }
        return true;
    } catch (IOException ex) {
        return false;
    }
}

And if you want to check if host is accessible via web/browser here is it:

private static boolean hostsWebApp(String host, int timeOutMillis) {
    boolean isReachable = isReachable(host, 80, timeOutMillis);
    if(!isReachable) {
        return isReachable(host, 443, timeOutMillis);
    } else {
        return true;
    }
}

Upvotes: 2

Alpha2k
Alpha2k

Reputation: 2241

I know this question has found its answer but I'd like to add my code just for "copy-paste".

public boolean isIpReachable(String ip, int timeout){
    boolean state = false;

    try {
        state = InetAddress.getByName(ip).isReachable(timeout);
    } catch (IOException e) {
        //Parse error here
    }

    return state;
}

Upvotes: 1

Paul Cager
Paul Cager

Reputation: 1920

It looks like you are using Linux so you will probably find that isReachable() is unreliable (because you will not have permissions to send ICMP packets, and very few servers have the Echo service running).

If that is the case then I think you will need to use spawn a Process in the way you suggest, but I recommend using a command like:

   ping -c 1 hostname

This will terminate after one attempt and you can then examine the exit status of the process - much more reliable than parsing standard output.

Ping returns 0 for success non-zero on failure.

Upvotes: 5

andypandy
andypandy

Reputation: 1117

InetAddress.getByName(ip).isReachable(timeout);

Upvotes: 55

michael667
michael667

Reputation: 3260

InetAddress.getByName(host).isReachable(timeOut) (seen here)

Upvotes: 12

Related Questions