7

I am working on a Java project which needs to have an internet connection at all times.

I want my program to keep checking the internet connection at some time intervals (say 5 or 10 seconds) and display a message as soon as no internet connection is detected.

I have tried to use the isReachable method to achieve this functionality, below is the code -

try
{
    InetAddress add = InetAddress.getByName("www.google.com");
    if(add.isReachable(3000)) System.out.println("Yes");
    else System.out.println("No");
}
catch (UnknownHostException e) 
{
    System.out.println("unkownhostexception");
} 
catch (IOException e) 
{
    System.out.println("IoException");
}

But this code always returns "No". What is the problem with this code?

Thanks

Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • 1
    http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address – uselpa Jun 09 '13 at 07:38
  • Thanks but the solution offered there, does not work in my case. the author too said its just a hack may not work in some cases. – Ankit Rustagi Jun 09 '13 at 07:45

2 Answers2

21

I agree with Raffaele's suggestion if you just need to use the internet frequently. Just try to do it, and be prepared to handle the exceptions when it fails.

If you need to monitor the availability of the internet for some reason, even when you don't intend to use it yet and don't know who you'll connect to, just try a connection to a known-good site (google.com, amazon.com, baidu.com... ).

This will work to test a few sites:

public static void main(String[] args) {
    System.out.println(
        "Online: " +
        (testInet("myownsite.example.com") || testInet("google.com") || testInet("amazon.com"))
    );
}


public static boolean testInet(String site) {
    Socket sock = new Socket();
    InetSocketAddress addr = new InetSocketAddress(site,80);
    try {
        sock.connect(addr,3000);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        try {sock.close();}
        catch (IOException e) {}
    }
}
maybeWeCouldStealAVan
  • 15,492
  • 2
  • 23
  • 32
  • 1
    yes, i am doing something similar. Thanks a lot ! – Ankit Rustagi Jun 09 '13 at 08:54
  • That's working, thanks a lot. The testInet() method is what does the trick. – joninx Sep 27 '17 at 13:53
  • I think this can be used for better error reporting. First you try accessing the service, but if it fails you can then check availability of other sites. This allows delivering a more meaningful error message to the user. Either "the remote service is currently unreachable" or "you currently have no internet connection". – scharette Oct 05 '22 at 18:10
11

The only way I am aware of is sending a packet to some known host, known in the sense that you know it will always be up and running and reachable from the calling site.

However, for example, if you choose a named host, the ping may fail due to a DNS lookup failure.

I think you shouldn't worry about this problem: if your program needs an Internet connection, it's because it sends or receives data. Connections are not a continuous concept like a river, but are more like a road. So just use the Java standard for dealing with connection errors: IOExceptiions. When your program must send data, the underlying API will certailnly throw an IOE in the case the network is down. If your program expects data, instead, use a timeout or something like that to detect possible errors in the network stack and report it to the user.

Raffaele
  • 20,627
  • 6
  • 47
  • 86