IOCP一

简介: // IOCP.cpp : Defines the entry point for the console application.////#include "stdafx.

// IOCP.cpp : Defines the entry point for the console application. // // #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #include <process.h> #pragma comment(lib, "WS2_32.lib") #define MAX_BUFFER 256 #define MAX_TIMEOUT 1000 #define MAX_SOCKET 1024 #define MAX_THREAD 64 typedef enum _OPERATION_INFO_ { OP_NULL, OP_READ, OP_WRITE }OPERATIONINFO; typedef struct _PER_HANDLE_DATA_ { public: _PER_HANDLE_DATA_() { clean(); } ~_PER_HANDLE_DATA_() { clean(); } protected: void clean() { sock = INVALID_SOCKET; memset(&addr, 0, sizeof(addr)); addr.sin_addr.S_un.S_addr = INADDR_ANY; addr.sin_port = htons(0); addr.sin_family = AF_INET; } public: SOCKET sock; SOCKADDR_IN addr; }PERHANDLEDATA, *PPERHANDLEDATA; typedef struct _PER_IO_DTATA_ { public: _PER_IO_DTATA_() { clean(); } ~_PER_IO_DTATA_() { clean(); } private: void clean() { ZeroMemory(&ol, sizeof(ol)); memset(buf, 0, sizeof(buf)); wsaBuf.buf = buf; wsaBuf.len = MAX_BUFFER; opType = OP_NULL; } public: WSAOVERLAPPED ol; WSABUF wsaBuf; char buf[MAX_BUFFER]; OPERATIONINFO opType; }PERIODATA, *PPERIODATA; HANDLE hThread[MAX_THREAD] = {0}; int g_nThread = 0; BOOL g_bExitThread = FALSE; unsigned __stdcall ThreadProc(LPVOID lParam); int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsaData; if(0 != WSAStartup(MAKEWORD(2, 2), &wsaData)) { printf("WSAStartup failed with error code: %d/n", GetLastError()); return EXIT_FAILURE; } if(2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion)) { printf("Socket version not supported./n"); WSACleanup(); return EXIT_FAILURE; } // Create I/O Completion Port HANDLE hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 0); if(NULL == hIOCP) { printf("CreateIoCompletionPort failed with error code: %d/n", GetLastError()); WSACleanup(); return EXIT_FAILURE; } // Create worker thread SYSTEM_INFO si = {0}; GetSystemInfo(&si); for(int i = 0; i < (int)si.dwNumberOfProcessors+2; i++) { hThread[g_nThread] = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, (LPVOID)hIOCP, 0, NULL); if(NULL == hThread[g_nThread]) { printf("_beginthreadex failed with error code: %d/n", GetLastError()); continue; } ++g_nThread; if(g_nThread > MAX_THREAD) { break; } } // Create socket SOCKET sListen = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); if(INVALID_SOCKET == sListen) { printf("WSASocket failed with error code: %d/n", WSAGetLastError()); goto EXIT_CODE; } SOCKADDR_IN addr; memset(&addr, 0, sizeof(addr)); addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); addr.sin_family = AF_INET; addr.sin_port = htons(5050); if(SOCKET_ERROR == bind(sListen, (LPSOCKADDR)&addr, sizeof(addr))) { printf("bind failed with error code: %d/n", WSAGetLastError()); closesocket(sListen); sListen = INVALID_SOCKET; goto EXIT_CODE; } if(SOCKET_ERROR == listen(sListen, 5)) { printf("listen failed with error code: %d/n", WSAGetLastError()); closesocket(sListen); sListen = INVALID_SOCKET; goto EXIT_CODE; } printf("Server start, wait for client to connect .../n"); while(TRUE) { SOCKADDR_IN remote; memset(&remote, 0, sizeof(remote)); int len = sizeof(remote); SOCKET sNew = WSAAccept(sListen, (LPSOCKADDR)&remote, &len, NULL, NULL); if(INVALID_SOCKET == sNew) { printf("WSAAccept failed with error code: %d/n", WSAGetLastError()); continue; } printf("Client

相关文章
iocp基础
iocp基础
85 0
|
Java Windows 容器
IOCP详解
IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型。它是应用程序使用线程池处理异步I/O请求的一种机制。在处理多个并发的异步I/O请求时,以往的模型都是在接收请求是创建一个线程来应答请求。这样就有很多的线程并行地运行在系统中。而这些线程都是可运行的,Windows内核花费大量的时间在进行线程的上下文切换,并没有多少时间花在线程运行上。再加上创建新线程的开销比较大,所以造成了效率的低下。
292 0
IOCP详解
|
Linux Windows
|
前端开发 算法 C++
IOCP编程小结(中)
上一篇主要谈了一些基本理念,本篇将谈谈我个人总结的一些IOCP编程技巧。   网络游戏前端服务器的需求和设计   首先介绍一下这个服务器的技术背景。在分布式网络游戏服务器中,前端连接服务器是一种很常见的设计。
1128 0
|
存储 Java API
[转载]IOCP模型的总结
原文:IOCP模型的总结IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型。它是应用程序使用线程池处理异步I/O请求的一种机制。在处理多个并发的异步I/O请求时,以往的模型都是在接收请求是创建一个线程来应答请求。
1041 0