IOCP(2)

### IOCP (Input/Output Completion Port) in Windows Networking and Server Applications IOCP is a powerful mechanism provided by Microsoft Windows for implementing scalable network servers that can handle large numbers of concurrent connections efficiently. This feature allows developers to perform asynchronous input/output operations without blocking threads while waiting for I/O completion. An Input/Output Completion Port associates one or more file handles with a completion port object, enabling multiple threads within the same process to wait simultaneously for I/O requests associated with these handles to complete[^1]. When using IOCP, instead of having dedicated threads per connection—which becomes inefficient as the number of connections grows—developers create a pool of worker threads that listen on an event queue managed by the operating system. Each time an operation completes, such as reading from or writing data into sockets, the OS places information about this completed task onto the queue where any available thread may pick up and continue processing immediately after receiving notifications through `GetQueuedCompletionStatus` API calls[^2]. For demonstration purposes, consider creating a simple echo server similar to what was described earlier but utilizing IOCP features specifically designed for high-performance scenarios under heavy loads: ```cpp #include <winsock2.h> #include <windows.h> #include <iostream> #pragma comment(lib,"ws2_32.lib") void HandleClient(LPVOID lpParameter); int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); SOCKET listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; service.sin_port = htons(5554); // Listening at TCP port 5554 bind(listeningSocket, (SOCKADDR*)&service, sizeof(service)); listen(listeningSocket, SOMAXCONN); HANDLE iocpHandle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); CreateIoCompletionPort((HANDLE)listeningSocket, iocpHandle, (ULONG_PTR)listeningSocket, 0); DWORD dwThreadIdArray[8]; HANDLE hThreadArray[8]; for(int i=0; i<8 ;i++) { hThreadArray[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)HandleClient, (LPVOID)iocpHandle , 0,&dwThreadIdArray[i]); } std::cout << "Server started." << std::endl; WaitForMultipleObjects(8, hThreadArray, TRUE, INFINITE); closesocket(listeningSocket); WSACleanup(); } DWORD WINAPI HandleClient(LPVOID lpParam){ HANDLE hIOCP = (HANDLE)lpParam; OVERLAPPED* overlapped; ULONG_PTR key; DWORD bytesTransferred; while(true){ GetQueuedCompletionStatus(hIOCP, &bytesTransferred, &key, &overlapped, INFINITE); if(key != INVALID_SOCKET && overlapped!=NULL){ // Process received message here... std::cout << "Received request..." << std::endl; // Send response back to client... send(((PER_IO_DATA*)overlapped)->socket,(char*)"Echo",strlen("Echo"),0); free(overlapped); } } } ``` This code snippet demonstrates setting up an IOCP-based server application capable of handling incoming connections asynchronously via a fixed-size thread pool rather than spawning new processes or threads dynamically upon each arrival. The core idea behind this approach lies in reusing existing resources effectively so they remain responsive even when dealing with thousands of simultaneous sessions over extended periods[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值