views:

144

answers:

4

How to write a java program which will tell me whether I have internet access or not. I donot want to ping or create connection with some external url because if that server will be down then my program will not work. I want reliable way to detect which will tell me 100% guarantee that whether I have internet connection or not irrespective of my Operating System. I want the program for the computers who are directly connected to internet.

I have tried with the below program

URL url = new URL("http://www.xyz.com/");

            URLConnection conn = url.openConnection();
            conn.connect();

I want something more appropriate than this program

Thanks Sunil Kumar Sahoo

+3  A: 

It depends on what you mean by "internet" connection. Many computers are not connected directly to the internet, so even if you could check whether they have a network connection, it doesn't always mean they can access the internet.

The only 100% reliable way to test whether the computer can access some other server is to actually try.

Dean Harding
+1 - for the last sentence.
Stephen C
Hi al thanx for your support I want the same thing for the computers who have direct internet access
Deepak
@Sunil - NO computer is directly connected to the internet. At the very least there is some kind of modem in between, probably with router and firewall capability. And that probably just connects to your ISP, not to the real internet.
Stephen C
Interesting philosophical question, @Stephen. What _is_ the internet. Is it just the top-tier communication providers? The wires (or other comms) between them? Or is it everything, down to your modem/router and even your PC? If I serve content to the world from my PC, surely it, and my modem, is part of the net as well.
paxdiablo
@paxdiablo - from the naive user's perspective, it is anything and everything involved in letting him talk to (say) Google, including the Google servers, his web browser and possibly even his computer's power switch!!
Stephen C
+1  A: 

Effective connectivity to the internet (i.e. where you can actually do stuff) depends on lots of things being correct, on your machine, your local net, your router, your modem, your ISP and so on. There are lots of places where a failure or misconfiguration will partly or completely block network access.

It is impossible to test all of these potential failure points with any certainty ... or even to enumerate them. (For example, you typically have no way of knowing what is happening inside your ISP's networking infrastructure.)

As @codeka says: "the only 100% reliable way to test whether the computer can access some other server is to actually try".

Stephen C
+1  A: 

I think if you were to open up a HTTP session with all of:

  • www.google.com
  • www.microsoft.com
  • www.ibm.com
  • www.ford.com

and at least one of them came back with a valid response, you would have internet connectivity. Don't keep testing once you get a valid response since that would be a waste.

Feel free to expand on that list with some more mega-corporations in case you fear that all four of them may be down at the same time :-)

Of course, even that method can be tricked if someone has taken control of your DNS servers but it's probably about as reliable as you're going to get.

paxdiablo
+1  A: 

Just put a try/catch block around the code you mentioned. If an exception is thrown/caught then you don't have connectivity.

boolean connectivity;
try {
    URL url = new URL("http://www.xyz.com/");
    URLConnection conn = url.openConnection();
    conn.connect();
    connectivity = true;
} catch (Exception e) {
    connectivity = false;
}

For better results investigate what kind of exceptions can be thrown and handle each individually.

Chris Nava