views:

2044

answers:

7

How do you check if you can connect to the internet via java? One way would be:

final URL url = new URL("http://www.google.com");
final URLConnection conn = url.openConnection();
... if we got here, we should have net ...

But is there something more appropriate to perform that task, especially if you need to do consecutive checks very often and a loss of internet connection is highly probable?

+6  A: 

You should connect to the place that your actual application needs. Otherwise you're testing whether you have a connection to somewhere irrelevant (Google in this case).

In particular, if you're trying to talk to a web service, and if you're in control of the web service, it would be a good idea to have some sort of cheap "get the status" web method. That way you have a much better idea of whether your "real" call is likely to work.

In other cases, just opening a connection to a port that should be open may be enough - or sending a ping. InetAddress.isReachable may well be an appropriate API for your needs here.

Jon Skeet
The link to isReachable went to the wrong class. It's at http://java.sun.com/javase/6/docs/api/java/net/InetAddress.html#isReachable
Chris Mazzola
what if you connect to a resource you want to communicate with and it fails. how do you know that it was the problem of the target server and not bcs there was no internet connection? so 'connect to the place that your actual application needs' is not really a solution that would not be a reliable check
Chris
@Chris: It depends whether you really care what the problem is. I agree that you can get more diagnostic information if (after a failure) you then connect somewhere else as well. But the most important piece of information is surely "Can I use the resource I'm trying to use?"
Jon Skeet
+2  A: 

This seems to be a duplicate of this question: Detect internet Connection using Java

Pascal Thivent
yes it is, didnt found that one. thxn!
Chris
Well, I don't get why people vote this answer down... but you're welcome.
Pascal Thivent
Well, there you got three upvotes in a minute!
BalusC
A: 

1) Figure out where your application needs to be connecting to.

2) Set up a worker process to check InetAddress.isReachable to monitor the connection to that address.

Justin Niessner
+4  A: 

People have suggested using INetAddress.isReachable. The problem is that some sites configure their firewalls to block ICMP Ping messages. So a "ping" might fail even though the web service is accessible.

And of course, the reverse is true as well. A host may respond to a ping even though the webserver is down.

And of course, a machine may be unable to connect directly to certain (or all) web servers due to local firewall restrictions.

The fundamental problem is that "can connect to the internet" is an ill-defined question, and this kind of thing is difficult to test without:

  1. information on the user's machine and "local" networking environment, and
  2. information on what the app needs to access.

So generally, the simplest solution is for an app to just try to access whatever it needs to access, and fall back on human intelligence to do the diagnosis.

Stephen C
well, not ill defined. the question is just "is there internet" nothing more.
Chris
That is my point. The question "is there internet" is ill-defined. Read the text above for examples that are intended to illustrate this.
Stephen C
@Chris: "Internet" is ill-defined. Being able to get to Google doesn't mean you can necessarily get anywhere else, for example. (Imagine if the DNS server is down, but you happen to have a DNS entry in your hosts file.)
Jon Skeet
And not being able to get to Google doesn't mean you cannot get to where the user actually wants to go.
Stephen C
@Stephen C: ok, now i got the point. maybe i have to try another way to accomplish my need. thxn for comments.
Chris
A: 

If you're on java 6 can use NetworkInterface to check for available network interfaces. I.e. something like this:

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
  NetworkInterface interf = interfaces.nextElement();
  if (interf.isUp() && !interf.isLoopback())
    return true;
}

Haven't tried it myself, yet.

Kutzi
A: 

URL url=new URL("http://[any domain]"); URLConnection con=url.openConnection();

/*now errors WILL arise here, i hav tried myself and it always shows "connected" so we'll open an InputStream on the connection, this way we know for sure that we're connected to d internet */

url.getInputStream();

//put the above statements in try catch blocks and if an exception in caught means that //there's no internet connection established. :-)

Rushil
A: 

I usually break it down into three steps.

  1. I first see if I can resolve the domain name to an IP address.
  2. I then try to connect via TCP (port 80 and/or 443) and close gracefully.
  3. Finally, I'll issue an HTTP request and check for a 200 response back.

If it fails at any point, I provide the appropriate error message to the user.

Marcus Adams