views:

277

answers:

4

I am developing a Java Desktop Application that access internet. It is a multi-threaded application, each thread do the same work (means each thread is an instance of same Thread class).

Now, as all the threads need internet connection to be active, there should be some mechanism that detects whether an internet connection is active or not.

Q1. How to detect whether the internet connection is active or not?

Q2. Where to implement this internet-status-check-mechanism code? Should I start a separate thread for checking internet status regularly and notifies all the threads when the status changes from one state to another? Or should I let each thread check for the internet-status itself?

Q3. This issue should be a very common issue as every application accessing an internet should deal with this problem. So how other developers usually deal with this problem?

Q4. If you could give me a reference to a good demo application that addresses this issue then it would greatly help me.

+1  A: 

A1: The only platform independent way of checking connection I know is to try to connect to reliable host (e.g. google.com)

A2: It depends on how frequently threads want to get information about connection. If there are many threads and they need to check connection every second, I'd prefer separate thread.

A3: There are platform-specific ways of solving the problem. Since you use Java, I think you shouldn't rely on them.

Dmitry
yes, I need only paltform-independent way to check internet-status
Yatendra Goel
A: 

If you cannot ask the operating system directly, you can create a socket connection to a known host. If the internet connection goes down in the computer itself that socket connection will be torn down and your program will be notified.

If you need to detect loss of connectivity "on the way" you must regularily receive data on the socket and keep track of whether this data arrive "on time". If not, the connection is unresponsive.

Thorbjørn Ravn Andersen
Even in "asking OS" method we have a problem: how should we distinguish connections to the Internet and to intranet?
Dmitry
+1  A: 

Check the connection to a number of reliable hosts, if they are all unreachable then you have more chances your internet connection being down than the hosts being down.

Bakkal
A: 

As others have mentioned you can poll a reliable host, another option is to use the java.net.NetworkInterface class and its method isUp(). A quick demonstration:

Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
    NetworkInterface ifc = ifcs.nextElement();
    System.out.println(ifc.getDisplayName()+ " "+ifc.isUp());
}

If the interface is disconnected then isUp should return false. Note that this will provide you with all interfaces so you should also check isLoopback() so you can ignore the loopback interface, which should always be up. This method should be more reliable than polling hosts, especially if your connection is slow, as well as being a more "courteous" method. At the very least you can use this as an initial test and if the interface does report itself as being up, then you can try to connect to a host.

M. Jessup
Note that "interface is up" doesn't equal to "connection to the Internet is active". So, it can only be used as pre-check.
Dmitry