WSAEventSelect模型基础 VS2010 、C

  • m6_292509
    了解作者
  • 7.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 10:12
    上传日期
WSAEventSelect模型基础 客户端向服务器发送消息 VS2010 C
WSAEventSelect模型基础 客户端向服务器发送消息 VS2010.rar
  • WSAEventSelectClient
  • WSAEventSelectClient.vcxproj.user
    143B
  • WSAEventSelectClient.cpp
    2.3KB
  • WSAEventSelectClient.vcxproj.filters
    958B
  • WSAEventSelectClient.vcxproj
    3.9KB
  • WSAEventSelectService
  • WSAEventSelectService.vcxproj.filters
    959B
  • WSAEventSelectService.vcxproj.user
    143B
  • WSAEventSelectService.cpp
    5.5KB
  • WSAEventSelectService.vcxproj
    3.9KB
  • WSAEventSelectService.sln
    1.4KB
内容介绍
#if 1 #include <stdio.h> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") int main() { WSADATA wsaData; WSAStartup(0x0202, &wsaData); SOCKET sListen; sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); unsigned short nPort = 8088; sockaddr_in addrListen; addrListen.sin_family = AF_INET; addrListen.sin_port = nPort; addrListen.sin_addr.S_un.S_addr = htonl(ADDR_ANY); int nErrorCode = 0; nErrorCode = bind(sListen, (sockaddr*)&addrListen, sizeof(sockaddr_in)); nErrorCode = listen(sListen, 5); WSAEVENT eventArray[WSA_MAXIMUM_WAIT_EVENTS]; SOCKET socketArray[WSA_MAXIMUM_WAIT_EVENTS]; int nEventTotal = 0; WSAEVENT eventCreate = WSACreateEvent(); nErrorCode = WSAEventSelect(sListen, eventCreate, FD_ACCEPT|FD_CLOSE); eventArray[nEventTotal] = eventCreate; socketArray[nEventTotal] = sListen; nEventTotal ++; printf("Service Started!\n"); while(TRUE) { int nIndex = WSAWaitForMultipleEvents(nEventTotal, eventArray, FALSE, WSA_INFINITE, FALSE); nIndex = nIndex - WSA_WAIT_EVENT_0; for (int i = nIndex; i<nEventTotal; i++) { nIndex = WSAWaitForMultipleEvents(1, &eventArray[i], TRUE, 1000, FALSE); if ((WSA_WAIT_FAILED == nIndex) || (WSA_WAIT_TIMEOUT == nIndex)) { continue; } else // 有新的连接 { WSANETWORKEVENTS eventNet; WSAEnumNetworkEvents(socketArray[i], eventArray[i], &eventNet); // 检测网络事件 if (eventNet.lNetworkEvents & FD_ACCEPT) { if (0 == eventNet.iErrorCode[FD_ACCEPT_BIT]) { if (nEventTotal>WSA_MAXIMUM_WAIT_EVENTS) { printf("Too many connections!\n"); continue; } SOCKET sAccept = accept(socketArray[i], NULL, NULL); // 新的连接 WSAEVENT eventNew = WSACreateEvent(); // 新的事件 WSAEventSelect(sAccept, eventNew, FD_READ|FD_WRITE|FD_CLOSE); eventArray[nEventTotal] = eventNew; socketArray[nEventTotal] = sAccept; nEventTotal++; } } else if (eventNet.lNetworkEvents & FD_READ) { if (0 == eventNet.iErrorCode[FD_READ_BIT]) { char chRecv[256]; int nRecv = recv(socketArray[i], chRecv, sizeof(chRecv), 0); if (nRecv > 0) // >0 接收到数据大小 { chRecv[nRecv] = '\0'; printf("--RECV:%s\n", chRecv); } } } else if (eventNet.lNetworkEvents & FD_WRITE) { } else if (eventNet.lNetworkEvents & FD_CLOSE) { if (0 == eventNet.iErrorCode[FD_CLOSE_BIT]) { closesocket(socketArray[i]); for (int j = 0; j<nEventTotal-1; j++) { eventArray[j] = eventArray[j+1]; socketArray[j] = socketArray[j+1]; } } nEventTotal--; } } } } return 0; } #else #include <WinSock2.h> #include <iostream> using namespace std; #pragma comment(lib, "WS2_32.lib") int main() { WSAEVENT eventArray[1024]; SOCKET sockArray[1024]; int nEventTotal = 0; WSADATA wsadata; WORD sockVersion = MAKEWORD(2, 2); WSAStartup(sockVersion, &wsadata); SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); unsigned short nPort = 8088; sockaddr_in addrListen; addrListen.sin_family = AF_INET; addrListen.sin_port = nPort; addrListen.sin_addr.S_un.S_addr = htonl(ADDR_ANY); if (bind(s, (sockaddr*)&addrListen, sizeof(addrListen)) == SOCKET_ERROR) { cout << "error" << endl; return 0; } listen(s, 5); WSAEVENT event = WSACreateEvent(); WSAEventSelect(s, event, FD_ACCEPT|FD_CLOSE); eventArray[nEventTotal] = event; sockArray[nEventTotal] = s; nEventTotal++; while(TRUE) { // 在所有事件对象上等待 int nIndex = WSAWaitForMultipleEvents(nEventTotal, eventArray, FALSE, WSA_INFINITE, FALSE); // 对每个事件调用WSAWaitForMultipleEvents函数,以便确定它的状态 nIndex = nIndex - WSA_WAIT_EVENT_0; for(int i=nIndex; i<nEventTotal; i++) { nIndex = WSAWaitForMultipleEvents(1, &eventArray[i], TRUE, 1000, FALSE); if(nIndex == WSA_WAIT_FAILED || nIndex == WSA_WAIT_TIMEOUT) { continue; } else { // 获取到来的通知消息,WSAEnumNetworkEvents函数会自动重置受信事件 WSANETWORKEVENTS event; WSAEnumNetworkEvents(sockArray[i], eventArray[i], &event); if(event.lNetworkEvents & FD_ACCEPT) // 处理FD_ACCEPT通知消息 { if(event.iErrorCode[FD_ACCEPT_BIT] == 0) { if(nEventTotal > WSA_MAXIMUM_WAIT_EVENTS) { cout << " Too many connections! " << endl; continue; } SOCKET sNew = accept(sockArray[i], NULL, NULL); WSAEVENT event = ::WSACreateEvent(); ::WSAEventSelect(sNew, event, FD_READ|FD_CLOSE|FD_WRITE); // 添加到表中 eventArray[nEventTotal] = event; sockArray[nEventTotal] = sNew; nEventTotal++; } } else if(event.lNetworkEvents & FD_READ) // 处理FD_READ通知消息 { if(event.iErrorCode[FD_READ_BIT] == 0) { char szText[256]; int nRecv = ::recv(sockArray[i], szText, 256, 0); if(nRecv > 0) { szText[nRecv] = 0; cout << "接收到数据:" << szText << endl; } } } else if(event.lNetworkEvents & FD_CLOSE) // 处理FD_CLOSE通知消息 { if(event.iErrorCode[FD_CLOSE_BIT] == 0) { ::closesocket(sockArray[i]); for(int j=i; j<nEventTotal-1; j++) { eventArray[j] = eventArray[j+1]; sockArray[j] = sockArray[j+1]; } nEventTotal--; i--; } } else if(event.lNetworkEvents & FD_WRITE) // 处理FD_WRITE通知消息 { } } } } return 0; } #endif
评论