开发者

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

开发者 https://www.devze.com 2022-12-29 21:18 出处:网络
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 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 Exc开发者_Python百科eption{
    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!


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.


The correct answer is not actually correct I think. Microsoft.com simply ignore ICMP requests, probably to avoid basic ping flood attacks. As for the second host I've no idea what the problem with the ping might be, but I'm using GNU/Linux and isReachable works just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号