Penchant
Penchant

Reputation: 1165

How do I test the availability of the internet in Java?

I don't want to tell the hard way that the internet is unavailable when I catch an exception from url.openStream().

Is there a simple way to tell if the computer is connected to the internet in Java? In this scenario, "connected to the internet" means being able to download data from a specific url.

If I try to download from it and it is not available, then the program hangs for a bit. I dont want that hanging. Therefore, i need a fast way of querying whether the website is available or not.

Upvotes: 8

Views: 12059

Answers (9)

Naga Rishi
Naga Rishi

Reputation: 51

Use

Process p1 = java.lang.Runtime.getRuntime().exec("ping www.google.com");
System.out.println(p1.waitFor());
// return code for p1 will be 0 if internet is connected, else it will be 1

Upvotes: 5

Peter Lawrey
Peter Lawrey

Reputation: 533880

If you program is hanging while waiting to download then you are probably using the GUI thread to do the download. If you use a background thread, your program won't hang even if it take a long time to timeout/download.

Upvotes: 1

Steve Weet
Steve Weet

Reputation: 28422

The problem you are trying to avoid is waiting for for your http connection to determine that the URL you are trying to access is really unavailable. In order to achieve this you need to stop using url.openStream() which is a shortcut for openConnection().getInputStream() and get some finer control over your connection.

URLConnection conn = url.openConnection();  
conn.setConnectTimeout(timeoutMs);  
conn.setReadTimeout(timeoutMs);  
in = conn.getInputStream();  

This code will allow you to timeout the connection attempt if either the connection or the read exceeds the time you provide in the timeoutMs paramater.

Upvotes: 10

If the criteria is whether you can access a given web server the only way to find out, is to access that web server.

You can, however, do it in a background thread at start up so the application is not delayed by it. Then the "yes/no" answer is available when the actual downloading is desired. Put a message in the status line so the user knows what is going on and is not suprised about uninitiated "connect to network" if s/he is not connected when your program is started.

Upvotes: 2

Chris
Chris

Reputation: 28064

I don't know much about the low level HTTP plumbing available in Java, but you could always issue an http HEAD request for the url in advance. This causes the server to send back only the headers and not the data. With a 1-3 second timeout, you should be able to avoid any lengthy delays.

Upvotes: 1

FerranB
FerranB

Reputation: 36837

You can check for a NTP server if your firewall does not forbids.

Any way I think the best option is to try to open an URL as you try to avoid, because is the service most commonly to be opened.

You can check for one or two dynamic URL that change constantly. For instace, some web that shows time. For instance this one i found googleing. More than one because the sites can be down ;-)

Upvotes: 0

dicroce
dicroce

Reputation: 46830

You could send an ICMP message to a very well known host...

Upvotes: 0

Anonymous
Anonymous

Reputation: 3051

There is no such thing as "Internet availability". Imagine that your HTTP request go through a transparent HTTP proxy, you are trying to access a blacklisted site and you get a HTTP response from the proxy server telling you about access denied. Is Internet available in this scenario or not?

I think you shall be more specific about your problem.

Upvotes: 4

cletus
cletus

Reputation: 625447

You might be able to tell if a compouter is connected to a network but even if it is there's no guarantee the networking is working or that it's connected to the internet.

This is one of those "suck it and see" problems.

What's wrong with trying to connect? And why are you concerned whether or not they're connected to the internet?

Upvotes: 2

Related Questions