Windows的提供了一套使用COM+接口的机制SENS(System Event Notification Services)用来让用户获得一系列的事件(见MSDN),不过我没有做过。请看MSDN中的这篇文章: SENS Architecture The System Event Notification Service works with the COM+ Event System. SENS is an event publisher for the classes of events that it monitors: network, logon, and power/battery events. The application receiving a notification is called an event subscriber.When an application subscribes to receive notifications, it can also specify filters associated with the subscribed events. SENS and COM+ Events use the filters to further determine when the application should be notified.Notifications are asynchronous, so the application receiving the notification does not have to be active when the notification is sent. When an application subscribes to receive notifications, it can specify whether it should be activated when the event occurs or notified later when it is active.The subscription can be transient and valid only until the application stops running, or it can be persistent and valid until the application is removed from the system.A COM+ Events data store contains information about the event publisher (SENS), event subscribers, and filters. During setup when you install or upgrade to Microsoft® Windows® 2000, SENS adds itself to the COM+ Events data store and provides information on the classes of events that it monitors using a GUID for each class of events. SENS also predefines an outgoing interface for each event class in a type library.Event class GUID Interface Network events SENSGUID_EVENTCLASS_NETWORK ISensNetwork Logon events SENSGUID_EVENTCLASS_LOGON ISensLogon Power events SENSGUID_EVENTCLASS_ONNOW ISensOnNow To receive notifications for any of these events, your application must do two things: Subscribe to the SENS events that interest you. To subscribe to an event, use the IEventSubscription and IEventSystem interfaces in COM+ Events. You need to supply an identifier for the event classes and the SENS publisher identifier, SENSGUID_PUBLISHER. Subscriptions are on a per event level so the subscribing application must also specify which events within the class are of interest. Each event corresponds to a method in the interface corresponding to its event class.
Note Programmers using SENS on the Internet Explorer 5 platform should use only the IEventSubscription and IEventSystem COM+ Events interfaces. Create a sink object with an implementation for each interface that you handle. See ISensNetwork, ISensLogon, and ISensOnNow for more information about these interfaces and the events supported in each one. When one of the monitored events occurs, SENS processes each subscription with any associated filters and notifies the subscribers through the COM+ Event system.
然后还是:
如果客户端和服务器端都是你开发的话好办。用异步模式,网线拔掉的时候可以得到一个错误事件。使用WSAGETSELECTERROR 取得错误代码,好像是WSAENETDOWN吧。虽然笨了点,但可行。
感谢 smch(Ashes Of Time) 出的主意,感谢大家的支持。我的程序只广播UDP包,没有server端。因此无法靠和server连接中断判断网络的连接是否正常。但我的程序又得知道网络连接是否正常,以此来决定是否要断开重新连接后重起服务。-------------------------------------------
老大,你可以简单的加一个线程来检测呀,为什么你为了实现这个功能就不能多加一点东西呢?在WIN2K中网线拔掉的话肯定有一个事件给你。发生错误之后你就可以发一个消息给你的主线程。
---------------------------------------------------------------------------------
我就是想知道是哪个事件响应
在主线程中:
OVERLAPPED overLapped;
DWORD dwError;
//HANDLE handle;
HANDLE h2;
handle=CreateEvent(NULL, // SD
false, // reset type
false, // initial state
"IPChang.data" // object name
);
overLapped.hEvent=handle;
NotifyAddrChange(&h2,&overLapped); //创建一个子进程来等待消息
LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL;
SIZE_T dwStackSize = 0;
//LPTHREAD_START_ROUTINE lpStartAddress;
LPVOID lpParameter = (LPVOID)&handle;
DWORD dwCreationFlags = 0 ;
DWORD lpThreadId;
HANDLE THandle = CreateThread(lpThreadAttributes, // SD
dwStackSize, // initial stack size
WaitForEvent, //lpStartAddress,thread function
lpParameter, // thread argument
dwCreationFlags, // creation option
&lpThreadId // thread identifier
);子线程:
DWORD WINAPI WaitForEvent(LPVOID data){
HANDLE handle;
DWORD dwError;
handle=*(HANDLE*)data;
int OriDhcpNum,CurDhcpNum; while(1){ dwError=WaitForSingleObject(handle,1000);
if( dwError == WAIT_OBJECT_0)
{
//A需要判断是否有新的网络设备启用
unsigned long OutBufLenN;
PIP_ADAPTER_INFO AdaptersInfo;
int AdaptersNum;
DWORD dret;
OutBufLenN=1;
AdaptersInfo=(PIP_ADAPTER_INFO)malloc(OutBufLenN);
dret=GetAdaptersInfo(AdaptersInfo,&OutBufLenN);
AdaptersInfo=(PIP_ADAPTER_INFO)malloc(OutBufLenN);
dret=GetAdaptersInfo(AdaptersInfo,&OutBufLenN);
。以上程序在win2000/xp下测试可以检测到是否有新的网卡启用或者网卡的IP地址发生变化。没有试验过是否能监测到网线
不过这种方式对于无线上网的机器无效。
可以从设备驱动去考虑,或者有别的方法
SENS Architecture
The System Event Notification Service works with the COM+ Event System. SENS is an event publisher for the classes of events that it monitors: network, logon, and power/battery events. The application receiving a notification is called an event subscriber.When an application subscribes to receive notifications, it can also specify filters associated with the subscribed events. SENS and COM+ Events use the filters to further determine when the application should be notified.Notifications are asynchronous, so the application receiving the notification does not have to be active when the notification is sent. When an application subscribes to receive notifications, it can specify whether it should be activated when the event occurs or notified later when it is active.The subscription can be transient and valid only until the application stops running, or it can be persistent and valid until the application is removed from the system.A COM+ Events data store contains information about the event publisher (SENS), event subscribers, and filters. During setup when you install or upgrade to Microsoft® Windows® 2000, SENS adds itself to the COM+ Events data store and provides information on the classes of events that it monitors using a GUID for each class of events. SENS also predefines an outgoing interface for each event class in a type library.Event class GUID Interface
Network events SENSGUID_EVENTCLASS_NETWORK ISensNetwork
Logon events SENSGUID_EVENTCLASS_LOGON ISensLogon
Power events SENSGUID_EVENTCLASS_ONNOW ISensOnNow
To receive notifications for any of these events, your application must do two things: Subscribe to the SENS events that interest you. To subscribe to an event, use the IEventSubscription and IEventSystem interfaces in COM+ Events. You need to supply an identifier for the event classes and the SENS publisher identifier, SENSGUID_PUBLISHER. Subscriptions are on a per event level so the subscribing application must also specify which events within the class are of interest. Each event corresponds to a method in the interface corresponding to its event class.
Note Programmers using SENS on the Internet Explorer 5 platform should use only the IEventSubscription and IEventSystem COM+ Events interfaces. Create a sink object with an implementation for each interface that you handle. See ISensNetwork, ISensLogon, and ISensOnNow for more information about these interfaces and the events supported in each one.
When one of the monitored events occurs, SENS processes each subscription with any associated filters and notifies the subscribers through the COM+ Event system.
制作一个SOCKET程序,自己连接自己,但需要指定网卡IP地址。
m_ServerAddr.sin_addr.s_addr = inet_addr("192.168.101.21");//这里指定网卡IP地址
-----------------------------
如果是异步模式。那么网线拔掉之后将会接收到WSAECONNABORTED错误信息。
bool CChatServerDlg::OnClientEvent(WPARAM wParam,LPARAM lParam)
{
(WSAGETSELECTERROR(lParam)==WSAECONNABORTED)
...
}在WIN2K下通过了测试。
客户端和服务器端定义一组验证机制。
客户端定时发送验证测试包,
服务器会测试响应包。这验证<->响应二者之间设置一个超时值,如5S,
如果这个时间内没有收到响应包,则为网络故障!