5

I see the 24 similar questions asking about InetAddress.isReachable() but my instance never returns true, when the network is otherwise reachable, with the one exception of tests against 127.0.0.1.

if (InetAddress.getByName("google.com").isReachable(2000))
{
    System.out.println("visible");
}
else
{
    System.out.println("not visible");
}

and with permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

This code is started within a thread (of course). My App needs to know if a specific IP is accessible (not google, as in the above example). This behaves the same in the emulator.

Is there a better way to test network connectivity?

Stuart Siegler
  • 1,686
  • 4
  • 30
  • 38
  • The best way to test the availablity of any resource is to try to use it. The best way to detect whether any specific IP is accessible is to try to connect to it to do what you need to do. Don't use `isReachable()` as a prior test at all. It doesn't test the same things. – user207421 May 08 '13 at 23:59
  • You may be right, and an answer in this thread http://stackoverflow.com/a/2935468/664479 -- which you commented on -- says "seems that isReachable() never worked well on Android b/c it tries to use ICMP"... – Stuart Siegler May 09 '13 at 00:35
  • avoid local host checking – codercat Apr 16 '14 at 09:04
  • @codercat Avoid *all* host checking. It's pointless. You still have to deal with exceptions when you go to use the actual resource: *ergo* that's all you really need. – user207421 Jun 14 '20 at 05:37

0 Answers0