WSAEventSelect模型

EventSelect

WSAEventSelect function

The WSAEventSelect function specifies an event object to be associated with the specified set of FD_XXX network events.

int WSAAPI WSAEventSelect(
SOCKET s,
WSAEVENT hEventObject,
long lNetworkEvents
);

s

A descriptor identifying the socket.

hEventObject

A handle identifying the event object to be associated with the specified set of FD_XXX network events.

lNetworkEvents

A bitmask that specifies the combination of FD_XXX network events in which the application has interest.

WSAEventSelect(s, hEventObject, FD_READ|FD_WRITE);

Remark

The WSAEventSelect function is used to specify an event object, hEventObject, to be associated with the selected FD_XXX network events, lNetworkEvents. The socket for which an event object is specified is identified by the s parameter. The event object is set when any of the nominated network events occur.

CreateEvent、CloseEvent、SetEvent、ResetEvent

WSACreateEvent function

The WSACreateEvent function creates a new event object.

WSAEVENT WSAAPI WSACreateEvent(
);

Remark

The WSACreateEvent function creates a manual-reset event object with an initial state of nonsignaled.

WSACloseEvent function

The WSACloseEvent function closes an open event object handle.

BOOL WSAAPI WSACloseEvent(
WSAEVENT hEvent
);

WSASetEvent function

The WSASetEvent function sets the state of the specified event object to signaled.

BOOL WSAAPI WSASetEvent(
WSAEVENT hEvent
);

WSAResetEvent function

The WSAResetEvent function resets the state of the specified event object to nonsignaled.

BOOL WSAAPI WSAResetEvent(
WSAEVENT hEvent
);

WaitForMultipleEvents

WSAWaitForMultipleEvents function

The WSAWaitForMultipleEvents function returns when one or all of the specified event objects are in the signaled state, when the time-out interval expires, or when an I/O completion routine has executed.

DWORD WSAAPI WSAWaitForMultipleEvents(
DWORD cEvents,
const WSAEVENT *lphEvents,
BOOL fWaitAll,
DWORD dwTimeout,
BOOL fAlertable
);

cEvents

The number of event object handles in the array pointed to by lphEvents.

lphEvents

A pointer to an array of event object handles.

fWaitAll

If TRUE, the function returns when the state of all objects in the lphEvents array is signaled. If FALSE, the function returns when any of the event objects is signaled.

dwTimeout

The time-out interval, in milliseconds.

fAlertable

A value that specifies whether the thread is placed in an alertable wait state so the system can execute I/O completion routines. If TRUE, the thread is placed in an alertable wait state and WSAWaitForMultipleEvents can return when the system executes an I/O completion routine. If FALSE, the thread is not placed in an alertable wait state and I/O completion routines are not executed.

EnumNetworkEvents

WSAEnumNetworkEvents function

The WSAEnumNetworkEvents function discovers occurrences of network events for the indicated socket, clear internal network event records, and reset event objects (optional).

int WSAAPI WSAEnumNetworkEvents(
SOCKET s,
WSAEVENT hEventObject,
LPWSANETWORKEVENTS lpNetworkEvents
);

s

A descriptor identifying the socket.

hEventObject

An optional handle identifying an associated event object to be reset.

lpNetworkEvents

A pointer to a WSANETWORKEVENTS structure that is filled with a record of network events that occurred and any associated error codes.

Remark

The WSAEnumNetworkEvents function is used to discover which network events have occurred for the indicated socket since the last invocation of this function. It is intended for use in conjunction with WSAEventSelect, which associates an event object with one or more network events.

Example

#ifndef UNICODE
#define UNICODE
#endif #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h> // Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib") int main()
{
//-------------------------
// Declare and initialize variables
WSADATA wsaData;
int iResult; SOCKET SocketArray[WSA_MAXIMUM_WAIT_EVENTS], ListenSocket;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
WSANETWORKEVENTS NetworkEvents;
sockaddr_in InetAddr;
DWORD EventTotal = 0;
DWORD Index;
DWORD i; HANDLE NewEvent = NULL; // Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
wprintf(L"WSAStartup failed with error: %d\n", iResult);
return 1;
} //-------------------------
// Create a listening socket
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
wprintf(L"socket function failed with error: %d\n", WSAGetLastError() );
return 1;
} InetAddr.sin_family = AF_INET;
InetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InetAddr.sin_port = htons(27015); //-------------------------
// Bind the listening socket
iResult = bind(ListenSocket, (SOCKADDR *) & InetAddr, sizeof (InetAddr));
if (iResult != 0) {
wprintf(L"bind failed with error: %d\n", WSAGetLastError() );
return 1;
} //-------------------------
// Create a new event
NewEvent = WSACreateEvent();
if (NewEvent == NULL) {
wprintf(L"WSACreateEvent failed with error: %d\n", GetLastError() );
return 1;
} //-------------------------
// Associate event types FD_ACCEPT and FD_CLOSE
// with the listening socket and NewEvent
iResult = WSAEventSelect(ListenSocket, NewEvent, FD_ACCEPT | FD_CLOSE);
if (iResult != 0) {
wprintf(L"WSAEventSelect failed with error: %d\n", WSAGetLastError() );
return 1;
} //-------------------------
// Start listening on the socket
iResult = listen(ListenSocket, 10);
if (iResult != 0) {
wprintf(L"listen failed with error: %d\n", WSAGetLastError() );
return 1;
} //-------------------------
// Add the socket and event to the arrays, increment number of events
SocketArray[EventTotal] = ListenSocket;
EventArray[EventTotal] = NewEvent;
EventTotal++; //-------------------------
// Wait for network events on all sockets
Index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, FALSE);
Index = Index - WSA_WAIT_EVENT_0; //-------------------------
// Iterate through all events and enumerate
// if the wait does not fail.
for (i = Index; i < EventTotal; i++) {
Index = WSAWaitForMultipleEvents(1, &EventArray[i], TRUE, 1000, FALSE);
if ((Index != WSA_WAIT_FAILED) && (Index != WSA_WAIT_TIMEOUT)) {
WSAEnumNetworkEvents(SocketArray[i], EventArray[i], &NetworkEvents);
}
} //...
return 0;

最新文章

  1. iOS 底层框架的浅析
  2. 在node.js中使用mongose模块
  3. css浏览器窗口大小
  4. 使用Burpsuite破解Webshell密码
  5. Python python 基本语法
  6. MVC自我学起之MVCMusic开发中遇到问题:musicstore edit方法出错的原因和解决方法
  7. [上海] 携程 门票事业部 招聘.NET 架构师 2 名 - V2EX
  8. 编程算法 - 阅读完整的文件(C++)
  9. elementui左侧菜单栏刷新后还是原来的状态
  10. javascript给定了日期如何获得星期几
  11. [JAVA]JAVA遍历Map的几种方式
  12. Android开发日常-listview滚动方法梳理
  13. 转csdn某位同学的 感谢bmfont
  14. POJ 1459&amp;&amp;3436
  15. Js 手风琴效果
  16. Chapter 4 Left Outer Join in MapReduce
  17. Verilog笔记.3.有限状态机
  18. luoguP4457 [BJOI2018]治疗之雨 概率期望 + 高斯消元
  19. Thinkphp实现excel导出数据
  20. hdu 1226(同余搜索)

热门文章

  1. 树莓派项目(三) 数字识别树莓派3+python3.5+opencv3.3+tensorflow1.7+keras
  2. Passwords Gym - 101174E (AC自动机上DP)
  3. CLR调试时的sos.dll/clr.dll/mscorwks.dll/mscordacwks.dll等动态库的版本对应
  4. C# 请求数据 方式1
  5. TCP/IP协议族基本知识
  6. Trie学习笔记
  7. Windows curl开启注意事项
  8. jsch配置sftp服务器ssh免密登录
  9. 用go写一个简单的看门狗程序(WatchDog)
  10. Xamarin.FormsShell基础教程(6)Shell项目内容详情页面