Java - 如何通过java桌面应用程序检测互联网连接是否已断开?

发布于 2024-08-29 19:38:43 字数 427 浏览 4 评论 0原文

我正在开发一个访问互联网的 Java 桌面应用程序。它是一个多线程应用程序,每个线程执行相同的工作(意味着每个线程都是同一 Thread 类的实例)。

现在,由于所有线程都需要互联网连接才能处于活动状态,因此应该有某种机制来检测互联网连接是否处于活动状态。

Q1. 如何检测互联网连接是否活跃?

Q2. 在哪里实现此互联网状态检查机制代码?我是否应该启动一个单独的线程来定期检查互联网状态,并在状态从一种状态更改为另一种状态时通知所有线程?或者我应该让每个线程检查互联网状态本身?

Q3。这个问题应该是一个非常常见的问题,因为每个访问互联网的应用程序都应该处理这个问题。那么其他开发者通常是如何处理这个问题的呢?

Q4。如果您能给我一个解决此问题的优秀演示应用程序的参考,那么它将对我有很大帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-09-05 19:38:43

A1:我所知道的唯一独立于平台的检查连接的方法是尝试连接到可靠的主机(例如google.com)

A2:这取决于线程想要连接的频率获取有关连接的信息。如果有很多线程并且它们需要每秒检查连接,我更喜欢单独的线程。

A3:有一些特定于平台的方法可以解决问题。既然你使用Java,我认为你不应该依赖它们。

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.

灼疼热情 2024-09-05 19:38:43

检查与多个可靠主机的连接,如果它们全部无法访问,那么您的互联网连接中断的可能性比主机中断的可能性更大。

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.

不即不离 2024-09-05 19:38:43

如果无法直接询问操作系统,可以创建到已知主机的套接字连接。如果计算机本身的互联网连接中断,则套接字连接将被断开,并且您的程序将收到通知。

如果您需要检测“途中”的连接丢失,则必须定期在套接字上接收数据并跟踪该数据是否“按时”到达。如果不是,则连接无响应。

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.

绮烟 2024-09-05 19:38:43

正如其他人提到的,您可以轮询可靠的主机,另一种选择是使用 java.net.NetworkInterface 类及其方法 isUp()。快速演示:

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

如果接口断开连接,则 isUp 应返回 false。请注意,这将为您提供所有接口,因此您还应该检查 isLoopback() ,以便您可以忽略环回接口,该接口应该始终处于启动状态。这种方法应该比轮询主机更可靠,特别是当您的连接速度很慢时,并且是一种更“礼貌”的方法。至少您可以使用它作为初始测试,如果接口确实报告自己已启动,那么您可以尝试连接到主机。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文