7

I am trying to figure out how to tell if a particular ipaddress is available in my android app during debugging ( I haven't tried this on an actual device ).

From reading it appears that InetAddress.isReachable should do this for me.

Initially I thought that I could code something like:

InetAddress address = InetAddress.getByAddress( new byte[] { (byte) 192, (byte) 168, (byte) 254, (byte) 10 ); success = address.isReachable( 3000 );

This returns false even though I am reasonably sure it is a reachable address.

I found that if I changed this to 127, 0, 0, 1 it returned success.

My next attempt was same code, but I used the address I got from a ping of www.google.com ( 72.167.164.64 as of this writing ). No success.

So then I tried a further example:

int timeout = 2000;
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress address : addresses)
{
    if ( address.isReachable(timeout))
    {
       success = true; // just set a break point here
    }
}

I am relatively new to Java and Android so I suspect I am missing something, but I can't find anything that would indicate what that is.

2 Answers 2

14

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.

4
  • 2
    'it tries to establish connection to port 7, that's usually have no running services on modern systems' ... yes but that doesn't matter because it interprets 'connection refused' to port 7 as success, indicating that the host is there and TCP is up.
    – user207421
    Jul 6, 2010 at 3:30
  • 2
    many systems will not send 'connection refused' or 'port unreachable' messages
    – zed_0xff
    Jul 7, 2010 at 6:22
  • After hours of messing with this I realise it doesn't work. What a mess. Thanks for the answer though ... +1 Aug 18, 2020 at 16:28
  • @MarquisofLorne are you sure it tries to hit port 7? Thats the echo service. The documentation on this is terrible. Does that mean if it isnt trying to use ping/ICMP that the need for a device to be rooted is spurious? Aug 18, 2020 at 16:31
5

Thanks Zed, I also know that the address I am trying to get will return html, so I can do more than look at the socket. Here is what I ended up doing that works reasonably for my problem. The parameter pUrl has the address of a website in it. I chose 3 seconds for the timeout.

boolean result = false;
try
{
    HttpGet request = new HttpGet(pUrl.toString());

    HttpParams httpParameters = new BasicHttpParams();  

    HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpResponse response = httpClient.execute(request);

    int status = response.getStatusLine().getStatusCode();

    if (status == HttpStatus.SC_OK) 
    {
        result = true;
    }

}
catch (SocketTimeoutException e)
{
    result = false; // this is somewhat expected
}
2
  • but what if we want to connect to specific port as a checking?
    – gumuruh
    May 7, 2022 at 13:56
  • but that is not android compatible right?
    – gumuruh
    May 7, 2022 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.