代码之家  ›  专栏  ›  技术社区  ›  Mike Lowery

无法从应用程序ping主机,但可以使用其他方法

  •  1
  • Mike Lowery  · 技术社区  · 14 年前

    这是我的密码:

    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 回复  |  直到 14 年前
        1
  •  1
  •   Community c0D3l0g1c    4 年前

    我没有在这里测试答案,但它似乎是明智的。

    请参见: Android Debugging InetAddress.isReachable

    似乎isReachable()在Android b/c上从来没有很好地工作过它尝试使用ICMP,这通常需要根权限,然后它尝试建立到端口7的连接,这在现代系统上通常没有运行的服务。

    你最好通过建立TCP连接来检查你知道应该打开的端口的连通性。