开发者

Java - How to detect that the internet connection has got disconnected through a java desktop application?

开发者 https://www.devze.com 2022-12-26 21:28 出处:网络
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).

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.

开发者_JS百科

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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消