下面是使用
WSAEventSelect的一个简单的例子
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define LISTEN_PORT 5080
#define BUFF_SIZE 256
int main(void)
{
WSADATA wsaData;
int nRet;
SOCKET listen_sock;
SOCKADDR_IN listen_addr;
WSAEVENT startEvent;
WSAEVENT clientEvent;
SOCKET client_sock;
int event_num = 0;
int index;
SOCKADDR_IN client_addr;
int client_addr_len = sizeof(client_addr);
WSANETWORKEVENTS wsaNetworkEvents;
WSAEVENT event_array[WSA_MAXIMUM_WAIT_EVENTS];
SOCKET sock_array[WSA_MAXIMUM_WAIT_EVENTS];
char buff[BUFF_SIZE];
int accept_num = 0;
nRet = WSAStartup(MAKEWORD(2,2), &wsaData);
if(0 != nRet)
{
printf("WSAStartup() failed, error:%d.\n", WSAGetLastError());
return 1;
}
listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == listen_sock)
{
printf("socket() failed, error:%d.\n", WSAGetLastError());
goto rettag1;
}
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
listen_addr.sin_port = htons(LISTEN_PORT);
nRet = bind(listen_sock, (SOCKADDR*)&listen_addr, sizeof(listen_addr));
if(nRet == SOCKET_ERROR)
{
printf("bind() failed, error:%d.\n", WSAGetLastError());
closesocket(listen_sock);
goto rettag1;
}
nRet = listen(listen_sock, 5);
if(SOCKET_ERROR == nRet)
{
printf("listen() failed, error:%d.\n", WSAGetLastError());
closesocket(listen_sock);
goto rettag1;
}
startEvent = WSACreateEvent();
if (startEvent == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed, error:%d.\n", WSAGetLastError());
closesocket(listen_sock);
goto rettag1;
}
nRet = WSAEventSelect(listen_sock, startEvent, FD_ACCEPT|FD_CLOSE);
if(SOCKET_ERROR == nRet)
{
printf("WSAEventSelect() failed, error:%d.\n", WSAGetLastError());
WSACloseEvent(startEvent);
closesocket(listen_sock);
goto rettag1;
}
event_array[event_num] = startEvent;
sock_array[event_num] = listen_sock;
++event_num;
while(1)
{
index = WSAWaitForMultipleEvents(event_num, event_array, FALSE, WSA_INFINITE, FALSE);
if(index == WSA_WAIT_FAILED || index == WSA_WAIT_TIMEOUT)
continue;
index -= WSA_WAIT_EVENT_0;
nRet = WSAEnumNetworkEvents(sock_array[index], event_array[index], &wsaNetworkEvents);
if(SOCKET_ERROR == nRet)
{
printf("WSAEnumNetworkEvents() failed, error:%d.\n", WSAGetLastError());
continue;
}
if(wsaNetworkEvents.lNetworkEvents & FD_ACCEPT)
{
if(wsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
{
printf("FD_ACCEPT error:%d.\n", wsaNetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
continue;
}
if(event_num >= WSA_MAXIMUM_WAIT_EVENTS)
{
continue;
}
client_sock = accept(sock_array[index], (SOCKADDR*)&client_addr, &client_addr_len);
if(INVALID_SOCKET == client_sock)
{
printf("in accept failed, error:%d.\n", WSAGetLastError());
continue;
}
clientEvent = WSACreateEvent();
if(clientEvent == WSA_INVALID_EVENT)
{
printf("in WSACreateEvent() failed, error:%d.\n", WSAGetLastError());
closesocket(client_sock);
continue;
}
nRet = WSAEventSelect(client_sock, clientEvent, FD_READ|FD_WRITE|FD_CLOSE);
if(SOCKET_ERROR == nRet)
{
printf("in WSAEventSelect() failed, error:%d.\n", WSAGetLastError());
closesocket(client_sock);
continue;
}
event_array[event_num] = clientEvent;
sock_array[event_num] = client_sock;
++event_num;
++accept_num;
}
else if(wsaNetworkEvents.lNetworkEvents & FD_READ)
{
if(wsaNetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ error:%d.\n", wsaNetworkEvents.iErrorCode[FD_READ_BIT]);
continue;
}
nRet = recv(sock_array[index], buff, BUFF_SIZE, 0);
if(nRet == SOCKET_ERROR)
{
printf("recv() failed, error:%d.\n", WSAGetLastError());
continue;
}
else if(nRet == 0)
{
printf("recv() 0.\n");
continue;
}
else
{
printf("\t%s", buff);
}
}
else if(wsaNetworkEvents.lNetworkEvents & FD_WRITE)
{
if(wsaNetworkEvents.iErrorCode[FD_WRITE_BIT] != 0)
{
printf("FD_WRITE error:%d.\n", wsaNetworkEvents.iErrorCode[FD_WRITE_BIT]);
continue;
}
printf("FD_WRITE do.\n");
}
else if(wsaNetworkEvents.lNetworkEvents & FD_CLOSE)
{
if(wsaNetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE error:%d.\n", wsaNetworkEvents.iErrorCode[FD_CLOSE_BIT]);
}
printf("\nFD_CLOSE!\n");
closesocket(sock_array[index]);
WSACloseEvent(event_array[index]);
while(index+1 < event_num)
{
sock_array[index] = sock_array[index+1];
event_array[index] = event_array[index+1];
++index;
}
--event_num;
if (event_num <= accept_num)
break;
}
}
while(event_num > 0)
{
closesocket(sock_array[event_num]);
WSACloseEvent(event_array[event_num]);
--event_num;
}
rettag1:
nRet = WSACleanup();
if(0 != nRet)
{
printf("WSACleanup() failed, error:%d.\n", WSAGetLastError());
return 1;
}
return 0;
}