1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <winsock2.h> #include <windows.h> #include <string> #include <iostream> using namespace std;
#pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"kernel32.lib")
HANDLE g_hIOCP;
enum IO_OPERATION{IO_READ,IO_WRITE};
struct IO_DATA{ OVERLAPPED Overlapped; WSABUF wsabuf; int nBytes; IO_OPERATION opCode; SOCKET client; };
char buffer[1024];
DWORD WINAPI WorkerThread (LPVOID WorkThreadContext) { IO_DATA *lpIOContext = NULL; DWORD nBytes = 0; DWORD dwFlags = 0; int nRet = 0;
DWORD dwIoSize = 0; void * lpCompletionKey = NULL; LPOVERLAPPED lpOverlapped = NULL;
while(1){ GetQueuedCompletionStatus(g_hIOCP, &dwIoSize,(LPDWORD)&lpCompletionKey,(LPOVERLAPPED *)&lpOverlapped, INFINITE);
lpIOContext = (IO_DATA *)lpOverlapped; if(dwIoSize == 0) { cout << "Client disconnect" << endl; closesocket(lpIOContext->client); delete lpIOContext; continue; }
if(lpIOContext->opCode == IO_READ) { ZeroMemory(&lpIOContext->Overlapped, sizeof(lpIOContext->Overlapped)); lpIOContext->wsabuf.buf = buffer; lpIOContext->wsabuf.len = strlen(buffer)+1; lpIOContext->opCode = IO_WRITE; lpIOContext->nBytes = strlen(buffer)+1; dwFlags = 0; nBytes = strlen(buffer)+1; nRet = WSASend( lpIOContext->client, &lpIOContext->wsabuf, 1, &nBytes, dwFlags, &(lpIOContext->Overlapped), NULL); if( nRet == SOCKET_ERROR && (ERROR_IO_PENDING != WSAGetLastError()) ) { cout << "WASSend Failed::Reason Code::"<< WSAGetLastError() << endl; closesocket(lpIOContext->client); delete lpIOContext; continue; } memset(buffer, NULL, sizeof(buffer)); } else if(lpIOContext->opCode == IO_WRITE) { lpIOContext->opCode = IO_READ; nBytes = 1024; dwFlags = 0; lpIOContext->wsabuf.buf = buffer; lpIOContext->wsabuf.len = nBytes; lpIOContext->nBytes = nBytes; ZeroMemory(&lpIOContext->Overlapped, sizeof(lpIOContext->Overlapped));
nRet = WSARecv( lpIOContext->client, &lpIOContext->wsabuf, 1, &nBytes, &dwFlags, &lpIOContext->Overlapped, NULL); if( nRet == SOCKET_ERROR && (ERROR_IO_PENDING != WSAGetLastError()) ) { cout << "WASRecv Failed::Reason Code1::"<< WSAGetLastError() << endl; closesocket(lpIOContext->client); delete lpIOContext; continue; } cout<<lpIOContext->wsabuf.buf<<endl; } } return 0; } void main () { WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET m_socket = WSASocket(AF_INET,SOCK_STREAM, IPPROTO_TCP, NULL,0,WSA_FLAG_OVERLAPPED);
sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(6000); server.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
bind(m_socket ,(sockaddr*)&server,sizeof(server));
listen(m_socket, 8);
SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); int g_ThreadCount = sysInfo.dwNumberOfProcessors * 2;
g_hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,g_ThreadCount);
for( int i=0;i < g_ThreadCount; ++i){ HANDLE hThread; DWORD dwThreadId; hThread = CreateThread(NULL, 0, WorkerThread, 0, 0, &dwThreadId); CloseHandle(hThread); }
while(1) { SOCKET client = accept( m_socket, NULL, NULL ); cout << "Client connected." << endl;
if (CreateIoCompletionPort((HANDLE)client, g_hIOCP, 0, 0) == NULL){ cout << "Binding Client Socket to IO Completion Port Failed::Reason Code::"<< GetLastError() << endl; closesocket(client); } else { IO_DATA * data = new IO_DATA; memset(buffer, NULL ,1024); memset(&data->Overlapped, 0 , sizeof(data->Overlapped)); data->opCode = IO_READ; data->nBytes = 0; data->wsabuf.buf = buffer; data->wsabuf.len = sizeof(buffer); data->client = client; DWORD nBytes= 1024 ,dwFlags=0; int nRet = WSARecv(client,&data->wsabuf, 1, &nBytes, &dwFlags, &data->Overlapped, NULL); if(nRet == SOCKET_ERROR && (ERROR_IO_PENDING != WSAGetLastError())){ cout << "WASRecv Failed::Reason Code::"<< WSAGetLastError() << endl; closesocket(client); delete data; } cout<<data->wsabuf.buf<<endl; } } closesocket(m_socket); WSACleanup(); }
|