0

Currently I am using URL()

public boolean isInternetAvailable(){
    try {
    URL url = new URL("http://www.google.com");
    HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
    urlConnect.setConnectTimeout(5000);
    Object objData = urlConnect.getContent();
    return true;
    } catch (Exception e) {}
    return false;
}

But In the requirement, we don't want to use any URL. We want to ping localhost if connection is available than return true otherwise flase.

For nslookup I am using

try
    {
      InetAddress inetAddress = InetAddress.getByName("localhost");

      System.out.println("Host: " +inetAddress.getHostName());
      System.out.println("IP Address: " +inetAddress.getHostAddress());
      System.out.println("IP Address: " +inetAddress.isSiteLocalAddress());
    }
    catch (UnknownHostException e)
    {
      e.printStackTrace();
    }

But I am not understand how to check the connection availability with nslookup.

Please suggest best approach for it. Thanks

Nikhil Gupta
  • 194
  • 1
  • 11
  • 2
    How would checking that **localhost** is available prove that Internet is available? – Adam Michalik Dec 14 '15 at 17:01
  • Hi Adam, We can assume that DNS server is available all the time – Nikhil Gupta Dec 14 '15 at 17:22
  • 1
    Posible duplicate: http://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java – PeterMmm Dec 14 '15 at 18:54
  • Normally `nslookup` is a well-known name for a program on the local system, not a service. `nslookup` will do some DNS queries to the network or retrieve information from the local system (`hosts` file), but not gurantees Internet availability at all. If you wants to run `nslookup` from Java, first read is always the [API](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29). – PeterMmm Dec 14 '15 at 19:01
  • I don't want to use any domain name for checking internet availability. – Nikhil Gupta Dec 15 '15 at 06:06

2 Answers2

1

There are several catches to this question:

  1. Starting with the most specific one - connecting to localhost: even if your computer does not have a network card, it will be able to resolve localhost on a loopback interface and connect to itself (if there is an open port).
  2. Your DNS may be down/misconfigured, so you cannot resolve example.com but you can connect to it by IP (93.184.216.34) - does that mean than "internet is not available"?
  3. The firewall in your company may be blocking certain sites, but allowing other - does that mean than "internet is not available"?
  4. The server of example.com is down while all the other sites in the world work fine. Does that mean than "internet is available" or not?
  5. The firewall in your company may be allowing HTTP connections only on standard ports 80 and 443 and disallowing other. Thus, http://example.com connects, but http://example.com:12345 does not. Does that mean than "internet is available" or not?

So the only question you can actually ask is whether you can connect to a particular host on a particular port using its domain name and/or its IP address.

Community
  • 1
  • 1
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
1

Figured out a final solution using NetworkInterface:

Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();
    while(eni.hasMoreElements()) {
        Enumeration<InetAddress> eia = eni.nextElement().getInetAddresses();
        while(eia.hasMoreElements()) {
            InetAddress ia = eia.nextElement();
            if (!ia.isAnyLocalAddress() && !ia.isLoopbackAddress() && !ia.isSiteLocalAddress()) {
                if (!ia.getHostName().equals(ia.getHostAddress()))
                    return true;
            }
        }
    }

I have got solution from here:- Java Quickly check for network connection

Community
  • 1
  • 1
Nikhil Gupta
  • 194
  • 1
  • 11