Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isReachable in Java doesn't appear to be working quite the way it's supposed to

I'm using Clojure, but I can read Java, so this isn't a Clojure specific question. This doesn't even seem to be working from Java.

I'm trying to implement a bit of a 'ping' function using isReachable. The code I'm using is this:

(.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000)

Translated to Java by a good friend of mine:

public class NetTest {
  public static void main (String[] args) throws Exception{
    String host = "acidrayne.net";
    InetAddress a = InetAddress.getByName(host);

    System.out.println(a.isReachable(10000));
  }
}

Both of these return false. I suppose I must be doin' it wrong, but Google research is telling me differently. I'm confuzzled!

like image 344
Rayne Avatar asked Oct 14 '22 06:10

Rayne


1 Answers

Updated in response to comment that this is wrong:

Using Unix/Linux??

http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html says:

Linux/Unix, instead, supports an ICMP "ping" system call. So the implementation of java.net.InetAddress.isReachable() first tries to perform the "ping" system call**; if this fails, it falls back trying to open a TCP socket on [sic - to] port 7, as in Windows.

It turns out that in Linux/Unix the ping system call requires root privileges, so most of the times java.net.InetAddress.isReachable() will fail, because many Java programs are not run as root, and because the target address unlikely has the echo service up and running. Too bad.

The comment below from @EJP indicates the part of about the echo service is wrong, wrong wrong:

That's not correct. isReachable returns true if it gets a ConnectException trying to connect to port 7, as that proves that the host is up and able to send RST segments.

In cases like these, I use a packet sniffer like WireShark, tcpdump (WinDump on Windows) or snoop (Solaris) to confirm what is really happening on the wire.

like image 148
Bert F Avatar answered Oct 18 '22 13:10

Bert F