iocp服务器代码_tct&udp下载
时间: 2024-01-21 08:01:05 浏览: 154
IOCP(Input/Output Completion Ports)是一种Windows系统下的高性能网络编程方式,通过异步I/O模型和多线程技术来提升服务器的吞吐量和性能。下面是一个简单的IOCP服务器代码示例:
```c++
#include <iostream>
#include <Winsock2.h>
#include <Windows.h>
#pragma comment(lib, "ws2_32.lib")
#define MAX_BUFFER_SIZE 4096
#define SERVER_PORT 8080
struct ClientContext {
OVERLAPPED overlapped;
SOCKET socket;
CHAR buffer[MAX_BUFFER_SIZE];
WSABUF wsaBuf;
};
DWORD WINAPI ServerWorkerThread(LPVOID lpParam) {
HANDLE iocp = (HANDLE)lpParam;
DWORD numBytes = 0;
ULONG_PTR completionKey = 0;
LPOVERLAPPED overlapped = NULL;
ClientContext* clientContext = NULL;
while (TRUE) {
if (!GetQueuedCompletionStatus(iocp, &numBytes, &completionKey, &overlapped, INFINITE)) {
std::cout << "IOCP Error: " << GetLastError() << std::endl;
continue;
}
clientContext = CONTAINING_RECORD(overlapped, ClientContext, overlapped);
if (numBytes == 0) {
closesocket(clientContext->socket);
delete clientContext;
continue;
}
std::cout << "Received data: " << clientContext->buffer << std::endl;
// Process client request
// Send response to client
// Prepare for next I/O operation
ZeroMemory(&(clientContext->overlapped), sizeof(OVERLAPPED));
clientContext->wsaBuf.buf = clientContext->buffer;
clientContext->wsaBuf.len = MAX_BUFFER_SIZE;
DWORD flags = 0;
DWORD recvBytes = 0;
int ret = WSARecv(clientContext->socket, &(clientContext->wsaBuf), 1, &recvBytes, &flags, &(clientContext->overlapped), NULL);
if (ret == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
std::cout << "WSARecv Error: " << WSAGetLastError() << std::endl;
closesocket(clientContext->socket);
delete clientContext;
}
}
return 0;
}
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "Failed to initialize Winsock" << std::endl;
return -1;
}
// Create socket
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket == INVALID_SOCKET) {
std::cout << "Failed to create socket" << std::endl;
return -1;
}
// Bind socket
SOCKADDR_IN serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(listenSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cout << "Failed to bind socket" << std::endl;
return -1;
}
// Listen
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cout << "Failed to listen" << std::endl;
return -1;
}
// Create IOCP
HANDLE iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (iocp == NULL) {
std::cout << "Failed to create IOCP" << std::endl;
return -1;
}
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
int numThreads = systemInfo.dwNumberOfProcessors * 2;
for (int i = 0; i < numThreads; i++) {
HANDLE threadHandle = CreateThread(NULL, 0, ServerWorkerThread, iocp, 0, NULL);
CloseHandle(threadHandle);
}
// Associate listen socket with IOCP
CreateIoCompletionPort((HANDLE)listenSocket, iocp, 0, 0);
while (TRUE) {
// Accept incoming client connections
SOCKADDR_IN clientAddr;
int addrLen = sizeof(clientAddr);
SOCKET clientSocket = accept(listenSocket, (SOCKADDR*)&clientAddr, &addrLen);
if (clientSocket == INVALID_SOCKET) {
std::cout << "Failed to accept client connection" << std::endl;
continue;
}
// Register client socket with IOCP
ClientContext* clientContext = new ClientContext();
clientContext->socket = clientSocket;
ZeroMemory(&(clientContext->overlapped), sizeof(OVERLAPPED));
clientContext->wsaBuf.buf = clientContext->buffer;
clientContext->wsaBuf.len = MAX_BUFFER_SIZE;
DWORD flags = 0;
DWORD recvBytes = 0;
int ret = WSARecv(clientSocket, &(clientContext->wsaBuf), 1, &recvBytes, &flags, &(clientContext->overlapped), NULL);
if (ret == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
std::cout << "WSARecv Error: " << WSAGetLastError() << std::endl;
closesocket(clientSocket);
delete clientContext;
continue;
}
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
以上是一个简单的IOCP服务器代码示例,通过IOCP模型和多线程技术实现异步高性能的网络通信。服务器在循环中等待客户端连接请求,并通过AcceptEx函数接受客户端连接,并将相关的客户端Socket与IOCP关联。每个线程通过WaitForMultipleObjects函数等待来自客户端的I/O完成事件,并进行相应的处理。对于已连接的客户端,通过WSARecv函数接收客户端发送的数据,并根据具体业务逻辑进行处理,然后通过WSASend函数发送响应给客户端,最后继续等待下一个I/O操作的完成。这样的方式可以充分利用系统资源,提高服务器的并发性能和吞吐量。
阅读全文