无法从应用程序ping主机,但否则无法

时间:2010-07-05 15:45:15

标签: java android

这是我的代码:

InetAddress address =
  InetAddress.getByName("www.whyoceans.com");
if (!address.isReachable(3000)) {
  Toast.makeText(this,
    "Unable to ping host",
    Toast.LENGTH_SHORT).show();
}

isReachable总是失败,但我可以打开一个shell并ping它 很好(来自我的电脑和手机):

ping www.whyoceans.com
PING www.whyoceans.com (69.163.249.123) 56(84) bytes of data.
64 bytes from apache2-fritz.harmony.dreamhost.com (69.163.249.123):
icmp_seq=1 ttl=55 time=38.3 ms
64 bytes from apache2-fritz.harmony.dreamhost.com (69.163.249.123):
icmp_seq=2 ttl=55 time=40.9 ms
^C
--- www.whyoceans.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 38.319/39.654/40.989/1.335 ms

为什么呢?

1 个答案:

答案 0 :(得分:1)

我没有在这里测试过这个答案,但这似乎很合理。

请参阅:Android Debugging InetAddress.isReachable

  

似乎isReachable()从未在Android b / c上运行良好它尝试使用ICMP,通常需要root权限,然后它尝试建立与端口7的连接,这通常在现代系统上没有正在运行的服务。

     

您最好检查连接,建立与您知道应该打开的端口的TCP连接。

相关问题