Hey there,
I'm having some troubles with IOCP and receiving packets. First, here's the basics of what I do in my server socket class to accept connections:
CreateIoCompletionPort for the server socket, then initialize worker threads. WSACreateEvent(), then WSAEventSelect using the server socket, accept event, and the FD_ACCEPT definition. CreateThread to start the accept thread. In the accept thread, WSAWaitForMultipleEvents using accept event handle and socket. Reset the socket event using WSAEnumNetworkEvents. Obtain the client remote socket using WSAAccept. CreateIoCompletionPort for the client socket using the client socket, server's iocp handle, and client context.
What's expected is that I can then get the first packet receive (using WSARecv) after calling GetQueuedCompletionStatus (used with the server's iocp handle), but the server cannot get past GetQueuedCompletionStatus. I feel that I should be telling the server some how that I want to receive packets now, but I'm not sure how to do that.
Any tips would be a huge help. Thanks in advance.
Edit: Here's the code from the Accept Thread:
sockaddr_in clientIpAddress;
int addressLength = sizeof(clientIpAddress);
SOCKET clientSocket = WSAAccept(_wsaSocket, (sockaddr*)&clientIpAddress, &addressLength, NULL, NULL);
cout << "<DEBUG> Incoming Connection from " << inet_ntoa(clientIpAddress.sin_addr) << endl;
if (clientSocket == INVALID_SOCKET)
{
printf("WSAAccept failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
return;
}
Client client(clientSocket, &clientIpAddress);
if (CreateIoCompletionPort((HANDLE)clientSocket, _iocpHandle, (DWORD)&client, 0) == NULL)
{
printf("CreateIoCompletionPort failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
return;
}
And here's the code from the Worker Thread:
if (!GetQueuedCompletionStatus(server->GetIOCPHandle(), &bytesTransferred,
(LPDWORD)&clientPtr, &overlappedPtr, INFINITE))
{ printf("GetQueuedCompletionStatus failed: %d\n", GetLastError()); continue; }
cout << "<DEBUG> Test Point Hit.\n";
if (clientPtr == NULL) break;
Client* client = (Client*)clientPtr;
If I put the WSARecv in the accept thread, it will make GetQueuedCompletionStatus work, but I can't have the initial receive in the accept thread for my application. Thanks again for your time.