windows 捕获网络状态变化事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:47:46  来源:igfitidea点击:

Capturing network status change event

c++windowsnetwork-programmingwxwidgetsconnectivity

提问by mentat

I am trying to get events when the internet connection is reestablished after it is lost. It is for a data transfer software that I am developing. If I lose the network during data transfer, I would like to be notified when it is back and continue the transfer automatically.

我试图在互联网连接丢失后重新建立时获取事件。它用于我正在开发的数据传输软件。如果我在数据传输过程中丢失网络,我希望在它恢复时收到通知并自动继续传输。

I can of course create a separate thread and check the network once in a while with a timer, but maybe there is a better option out there.

我当然可以创建一个单独的线程并使用计时器不时检查网络,但也许有更好的选择。

I am developing for windows mainly, in C++ (not .net).

我主要使用 C++(不是 .net)为 Windows 开发。

I can also use wxwidgets (I use it for GUI) but I doubt it offers any related functionality.

我也可以使用 wxwidgets(我将它用于 GUI),但我怀疑它是否提供任何相关功能。

采纳答案by Ruddy

You might want to check out the System Event Notification Server (SENS) API http://msdn.microsoft.com/en-us/library/cc185680(VS.85).aspx

您可能想查看系统事件通知服务器 (SENS) API http://msdn.microsoft.com/en-us/library/cc185680(VS.85).aspx

I have not actually used it, but it seems like it supplies the events your looking for.

我实际上并没有使用它,但它似乎提供了您正在寻找的事件。

EDIT:

编辑:

WMI appears to have all the information you need about various network connectivity and state changes. It also has an asynchronous event model that can be used to get notifications. The trick is, i suppose, generating the proper WMI query to get the information you want. This bloglooks like the right type of query, and this MSDNexplains how to handle the events asynchronously.

WMI 似乎拥有您需要的有关各种网络连接和状态更改的所有信息。它还具有可用于获取通知的异步事件模型。我想,诀窍是生成正确的 WMI 查询来获取您想要的信息。这个博客看起来是正确的查询类型,这个MSDN解释了如何异步处理事件。

回答by FuzzyAmi

This MSDN link gives a very detailed example of how to capture events on WMI with COM. The example doesn't actually capture network events - but I believe that if you plug the right query in, it would work.

此 MSDN 链接提供了一个非常详细的示例,说明如何使用 COM 捕获 WMI 上的事件。该示例实际上并未捕获网络事件 - 但我相信如果您插入正确的查询,它将起作用。

(lots of code here, so I'm not copying it into the answer) http://msdn.microsoft.com/en-us/library/aa390425%28v=vs.85%29.aspx

(这里有很多代码,所以我没有将其复制到答案中) http://msdn.microsoft.com/en-us/library/aa390425%28v=vs.85%29.aspx

回答by stefaanv

I don't know which protocol you use and whether you can control the destination, but in that case, the destination can poll for a retry. The destination knows best what it has received, so it can give the received number of bytes as offset for the retransmission.

我不知道您使用哪种协议以及您是否可以控制目的地,但在这种情况下,目的地可以轮询重试。目的地最清楚它接收到的内容,因此它可以将接收到的字节数作为重传的偏移量。