Unable to ping host from app but can otherwise
Here's my code:
InetAddress address =
InetAddress.getByName("www.whyoceans.com");
if (!address.isReachable(3000)) {
Toast.makeText(this,
"Unable to ping host",
Toast.LENGTH_SHORT).show();
}
The isReachable always fails, yet I can open a shell and ping it just fine (from both my PC and my phone):
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 开发者_StackOverflow社区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
Why?
I haven't tested the answer here but it seems sensible.
See: 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.
精彩评论