1

I have seen that this question had been asked before in SO. Some of the threads are:

All these threads are pretty old. The approaches that are defined there are:

  • Opening an HttpURLConnection by openConnection().
  • Checking InetAddress.isReachable.
  • Executing ping command through Process and processing the output.

I have seen if I use the second approach to check the connectivity with www.google.com then it is returning false, which should not be the result. But the first way works. I cannot use the last way since the respondent himself said it.

I have also seen the following way:

private boolean isConnected(Socket socket, String address) {
    if(socket == null) {
        throw new NullPointerException("Socket cannot be null");            
    }

    try {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(address, 80);
        socket.connect(inetSocketAddress);
        return true;
    } catch (Throwable cause) {
        LOGGER.error(cause.getMessage(), cause);
        return false;
    }
}

By this way I am getting right output. The aforementioned threads mentioned that there is no proper way to validate if the computer is connected to the internet or not. But since these threads are old so I am hoping there might be some new way out there by which I can achieve what I want. Also I have to consider that there are various ways to access internet like LAN, Broadband, Dial Up, DSL etc and some server might block ping access or can block some IP.

Any pointer would be very helpful.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • There isno such thing as "connected to the Internet" in general. You can only check whether the host/port combination (or IP address) you're trying to reach is reachable. If it is, it does not guarantee that any other host/port is reacheable. Consider things like Great Firewall of China. Are you connected to "the Internet" when you're in there? –  Apr 22 '14 at 12:06
  • Trying to connect to google.com can fail if your DNS server is not set correctly. It might be safer to ping an IP address instead. The best options IMO are DNS servers, they rarely change their IP address and they are pretty reliable. For example the Google DNS: 8.8.8.8 . – user1793963 Apr 22 '14 at 13:12

1 Answers1

0

You could use the NetworkInterface class, it has a isUp() method which returns boolean indicating whether the particular interface is up and running (which could indicate if it's used for internet connection).

API: http://docs.oracle.com/javase/7/docs/api/java/net/NetworkInterface.html

agg.ie
  • 11
  • 3
  • Thanks for your response. So far I know the NetworkInterface checks if the device(s) is functional, not the connectivity. It can be false positive. – Tapas Bose Apr 22 '14 at 12:52
  • Yeah, true. Though I found this website (quite new, from January) that has some code checking for internet connection http://www.javabeat.net/internet-connection-java/# – agg.ie Apr 22 '14 at 13:01