isReachable在Java中似乎不工作的方式,它应该是 [英] isReachable in Java doesn't appear to be working quite the way it's supposed to

查看:459
本文介绍了isReachable在Java中似乎不工作的方式,它应该是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Clojure,但我可以阅读Java,所以这不是一个Clojure的具体问题。这似乎不是从Java工作。



我试图使用isReachable实现一个'ping'函数。我使用的代码是:

 (。isReachable(java.net.InetAddress / getByNamewww.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支持ICMPping系统调用。所以java.net.InetAddress.isReachable()的实现首先尝试执行ping系统调用**;如果此操作失败,则会尝试在[sic-to]端口7 上打开TCP套接字,如Windows中所示。



在Linux / Unix中, ping系统调用需要root权限,因此大多数时候java.net.InetAddress.isReachable()将失败,因为许多Java程序不以root身份运行 ,并且因为目标地址不太可能启动并运行echo服务。太糟糕了。


@EJP下面的注释表示关于echo服务的部分是错误的,


这不正确。如果尝试连接到端口7的ConnectException,isReachable将返回true,因为这证明主机已启动并能够发送RST段。


在这种情况下,我使用一个数据包嗅探器,如WireShark,tcpdump(Windows上的WinDump)或snoop(Solaris)来确认电线上真正发生了什么。


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!

解决方案

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.

这篇关于isReachable在Java中似乎不工作的方式,它应该是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆