gpt4 book ai didi

java - 无法从应用程序 ping 主机,但可以

转载 作者:行者123 更新时间:2023-11-30 04:51:31 24 4
gpt4 key购买 nike

这是我的代码:

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

为什么?

最佳答案

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

参见:Android Debugging InetAddress.isReachable

seems that isReachable() never worked well on Android b/c it tries to use ICMP, that usually needs a root privileges, and then it tries to establish connection to port 7, that's usually have no running services on modern systems.

you'd better check connectivity with establishing TCP connection to ports you know should be open.

关于java - 无法从应用程序 ping 主机,但可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com