EventSelect網絡模式
阿新 • • 發佈:2017-07-06
printf def std hand val byte etl for init
#include "stdafx.h" #include<WinSock2.h> #include<windows.h> #include<iostream> using namespace std; #pragma comment (lib,"ws2_32.lib") #define PORT 6000 SOCKET ArrSocket[1024] = { 0 }; WSAEVENT ArrEvent[1024] = { 0 }; DWORD dwTotal = 0; DWORD dwIndex = 0; int ClientNums = 0; //表示有多少個客戶端bool InitNetEvn() { WSADATA wsa = {0}; if (WSAStartup(MAKEWORD(2,2),&wsa)!=0) { cout << "初始化網絡環境失敗" << endl; return false; } if (LOBYTE(wsa.wVersion)!=2||HIBYTE(wsa.wVersion)!=2) { cout << "網絡環境的版本號不正確" << endl; WSACleanup();return false; } return true; } DWORD WINAPI ThreadProc(LPVOID lpParameter) { cout << "進入客戶服務程序" << endl; char buf[1024] = { 0 }; SOCKET sockClient = INVALID_SOCKET; WSANETWORKEVENTS netWorkEvent = { 0 }; while (TRUE) { dwIndex = WSAWaitForMultipleEvents(dwTotal, ArrEvent, FALSE, 100, FALSE); if (dwIndex==WSA_WAIT_TIMEOUT) { continue; } WSAEnumNetworkEvents(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], ArrEvent[dwIndex - WSA_WAIT_EVENT_0], &netWorkEvent); if (netWorkEvent.lNetworkEvents&FD_ACCEPT) { if (netWorkEvent.iErrorCode[FD_ACCEPT_BIT]!=0) { continue; } sockClient = accept(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], NULL, NULL); if (sockClient==INVALID_SOCKET) { continue; } cout << "接受客戶端請求" << endl; WSAEVENT newEvent = WSACreateEvent(); WSAEventSelect(sockClient, newEvent, FD_READ | FD_WRITE | FD_CLOSE); int i; for (i=0;i<dwTotal;i++) { if (ArrSocket[i]==0) { ArrSocket[i] = sockClient; ArrEvent[i] = newEvent; break; } } if (i==dwTotal) { ArrSocket[dwTotal] = sockClient; ArrEvent[dwTotal] = newEvent; ++dwTotal; ++ClientNums; } } if (netWorkEvent.lNetworkEvents&FD_READ) { if (netWorkEvent.iErrorCode[FD_READ_BIT]!=0) { continue; } recv(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], buf, sizeof(buf), 0); printf("Server Recv:%s\n", buf); send(ArrSocket[dwIndex - WSA_WAIT_EVENT_0], buf, strlen(buf), 0); } if (netWorkEvent.lNetworkEvents&FD_WRITE) { if (netWorkEvent.iErrorCode[FD_WRITE_BIT]!=0) { continue; } printf("send something\n"); } if (netWorkEvent.lNetworkEvents&FD_CLOSE) { if (netWorkEvent.iErrorCode[FD_CLOSE_BIT]!=0) { continue; } closesocket(ArrSocket[dwIndex - WSA_WAIT_EVENT_0]); ArrSocket[dwIndex - WSA_WAIT_EVENT_0] = 0; } } } int main() { if (!InitNetEvn()) { return -1; } cout << "初始化成功" << endl; SOCKET socketListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (socketListen==INVALID_SOCKET) { cout << "創建套接字失敗" << endl; return -1; } cout << "創建套接字成功" << endl; SOCKADDR_IN si; si.sin_addr.S_un.S_addr = INADDR_ANY; si.sin_family = AF_INET; si.sin_port = htons(PORT); int ret = bind(socketListen, (sockaddr*)&si, sizeof(sockaddr)); if (ret==SOCKET_ERROR) { cout << "套接字綁定失敗"<<endl; return -1; } cout << "套接字綁定成功" << endl; WSAEVENT listenEvent = WSACreateEvent(); WSAEventSelect(socketListen, listenEvent, FD_CLOSE | FD_ACCEPT); HANDLE hThread= CreateThread(NULL, NULL, ThreadProc, NULL, NULL,NULL); if (hThread==NULL) { cout << "創建線程失敗" << endl; closesocket(socketListen); WSACleanup(); return -1; } ret = listen(socketListen, SOMAXCONN); if (ret==SOCKET_ERROR) { cout << "監聽失敗" << endl; return -1; } cout << "監聽成功" << endl; ArrSocket[dwTotal] = socketListen; ArrEvent[dwTotal] = listenEvent; ++dwTotal; system("pause"); if (socketListen!=INVALID_SOCKET) { closesocket(socketListen); } WSACleanup(); return 0; }
EventSelect網絡模式