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;

WSAEventSelect模型的更多相关文章

  1. WSAEventSelect模型详解

    WSAEventSelect 是 WinSock 提供的一种异步事件通知I/O模型,与 WSAAsyncSelect模型有些类似.       该模型同样是接收 FD_XXX 之类的网络事件,但是是通 ...

  2. winsock编程WSAEventSelect模型

    winsock编程WSAEventSelect模型 WSAEventSelect模型和WSAAsyncSelec模型类似,都是用调用WSAXXXXXSelec函数将socket和事件关联并注册到系统, ...

  3. WinSock WSAEventSelect 模型总结

    前言 本文配套代码:https://github.com/TTGuoying/WSAEventSelect-model 由于篇幅原因,本文假设你已经熟悉了利用Socket进行TCP/IP编程的基本原理 ...

  4. WSAEventSelect模型编程 详解

    转自:http://blog.csdn.net/wangjieest/article/details/7042108 WSAEventSelect模型编程 WSAEventSelect模型编程这个模型 ...

  5. WinSock WSAEventSelect 模型

    在前面我们说了WSAAsyncSelect 模型,它相比于select模型来说提供了这样一种机制:当发生对应的IO通知时会立即通知操作系统,并调用对应的处理函数,它解决了调用send和 recv的时机 ...

  6. 三.Windows I/O模型之事件选择(WSAEventSelect )模型

    1.事件选择模型:和异步选择模型类似的是,它也允许应用程序在一个或多个套接字上,接收以事件为基础的网络事件通知.对于异步选择模型采用的网络事件来说,它们均可原封不动地移植到事件选择模型.事件选择模型和 ...

  7. WinSock IO模型 -- WSAEventSelect模型事件触发条件说明

    FD_READ事件 l  调用WSAEventSelect函数时,如果当前有数据可读 l  有数据到达时,并且没有发送过FD_READ事件 l  调用recv/recvfrom函数后,仍然有数据可读时 ...

  8. 套接字I/O模型-WSAEventSelect(转载)

    和WSAAsyncSelect类似,它也允许应用程序在一个或多个套接字上,接收以事件为基础的网络事件通知. 该模型最主要的区别是在于网络事件是由对象句柄完成的,而不是通过窗口例程完成. 事件通知 事件 ...

  9. 套接字I/O模型之WSAEventSelect

    今天我又学习了一种新的套接字I/O模型------WSAEventSelect,他与WSAAsyncSelect一样也是一种异步事件通知模型,不同的是WSAAsyncSelect是与窗口句柄关联在一起 ...

随机推荐

  1. 第08组 Alpha冲刺(4/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11882079.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  2. eclipse右键空白、eclipse提示空白

    右键选择菜单经常显示空白.要试好多次才会出来.eclipse无法启动.启动报错 查看eclipse安装目录下的.metadata下的.log 里面会记录eclipse的报错信息 一般显示空白问题都是因 ...

  3. An internal error occurred during: "Synchronizing"

    An internal error occurred during: "Synchronizing" “同步”期间发生内部错误. 处理方法 :单个文件进行更新,将无法更新的文件进行 ...

  4. 【Alpha】“北航社团帮”小程序v1.0测试报告

    目录 测试计划.过程和结果 后端单元测试 后端压力测试 测试结果 指标解释 前端测试 授权登录与权限检查 功能测试 兼容性测试 性能测试 回答课程组问题 测试中发现的bug 场景测试 测试矩阵 出口条 ...

  5. 阿里云ECS服务器相关配置以及操作---上(初学者)

    最近买了一台阿里云的ECS服务器 linux系统 centos镜像,把我相关的一些操作记录下来,供大家参考,不足之处欢迎指正. 首先买的过程就不用介绍了,根据自己的实际需要选择自己想要的配置,点击付钱 ...

  6. Netty通信网络参数配置

    Netty服务端/客户端网络通信过程中常用的参数: Name Associated setter method "writeBufferHighWaterMark" 默认64 * ...

  7. Linux 设置代理

    一.为系统设置代理编辑文件/etc/profile,增加如下两行 export http_proxy=http://proxy.com:8080/export https_proxy=http://p ...

  8. C++11版本不能使用一个单行命名空间方式特化一个函数的bug

    warning: specialization of ‘template<class _Iterator> struct std::iterator_traits’ in differen ...

  9. SpringBoot 为API添加统一的异常处理(一)

    首先我把异常分为两种,一种是可控制的,或者是由我们发现条件不正确主动抛出的异常,就像前城市编号不存在那个粟子:另一种是不可控制的,或者说是程序存在bug引起的异常,但这种异常也不想变态的就直接给前端抛 ...

  10. redis创建集群至少需要几个节点?至少需要几个master节点?

    描述: 这也算个思考吧,通过redis-trib.rb可创建redis集群,然后通过--replicas后面接的数字,表示1个主节点对应几个从节点,那么我就做了如下的测试,想要达到的效果就是有6个节点 ...