Java中的isReachable看起来并没有像预期的那样工作

我正在使用Clojure,但我可以阅读Java,所以这不是Clojure特定的问题.这甚至似乎都不适用于Java.

我正在尝试使用isReachable实现一些’ping’功能.我正在使用的代码是这样的:

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

我的一位好朋友翻译成Java:

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));
  }
}

这两个都返回false.我想我一定是做错了,但谷歌的研究告诉我的不同之处.我很困惑!

最佳答案
更新以回应评论这是错误的:

使用Unix / Linux ??

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

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.

@EJP下面的评论表明关于echo服务的部分是错误的,错误错误

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.

在这些情况下,我使用WireShark,tcpdump(Windows上的WinDump)或snoop(Solaris)等数据包嗅探器来确认线路上发生了什么.

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...