WSAEventSelect model

WSAEventSelect model , allowing the application to accept the notification time-based network, but also to accept FD_XXX type of network events, rely on message-driven mechanisms and windows event objects associated in one or more sockets.

The basic idea : to create an event object as a set of network events of interest, call WSAEventSelect function to associate network events and event objects.

winsock create an event object function WSACreateEvent, definition:

WSAEVENT WSACreateEvent(void);//返回一个手工设置的事件对象句柄

After creation, calling WSAEventSelect function specified network events associated with it together

int WSAEventSelect(
                   SOCKET s,
                   WSAEVENT hEventObject,//事件对象句柄
                   long lNetWorkEvents//感兴趣的组合
                   );

WSAWaitForMultipleEvents function on one or more events waiting, while waiting for an event object is trusted, or specify the events in the past, the function returns

DWORD WSAWaitForMultipleEvents(
    DWORD cEvents,//指定下面所指数组中的事件对象句柄的个数
    const WSAEVENT* lphEvents,//指向事件对象句柄数组
    BOOL fWaitAll,//指定是否等待所有事件对象编程受信状态
    DWORD dwTimeout,//指定等待时间
    BOOL fAlertable//使用WSAEventSelect可以忽略
);

It supports up to WSA_MAXIMUM_WAIT_EVENTS objects, defined as 64.

WSAWaitForMultipleEvent S network events waiting to happen

过了指定事件:返回WSA_WAIT_TIMEOUT

指定事件内有网络事件发生:返回值指明发生事件对象

函数调用失败:返回WSA_WAIT_FAILED

dwTimeout value is 0, the state of the specified event object, return immediately to test whether trusted

After fWaitAll set to false, if there are several, you can only indicate one handle to the front of the ...

 

Trusted objects, find the appropriate socket, call WSAEnumNetWorkEvents function, you can see what network events occurred :

int WSAEnumNetWorkEvents(
    SOCKET s,
    WSAEVENTS hEventObject,
    LPWSANETWORKEVENTS lpNetworkEvents
);

The last parameter is used to obtain network events and the associated error code socket occurred :

typedef struct _WSANETWORKEVENTS{
    long lNetworkEvents;//指定发生的网络事件
    int iErrorCode[FD_MAX_EVENTS];//取得错误代码
}WSANETWORKEVENTS,*LPWSANETWORKEVENTS;

Reproduced in: https: //my.oschina.net/u/204616/blog/545009

Guess you like

Origin blog.csdn.net/weixin_34289454/article/details/91989642