In the code snippet below, I wasn't getting all the data being sent until I added the Sleep function. I'm wondering if there is a different way of doing this without using Sleep.
I tried the loops in several configurations and this is the best I could come up with.
// Make the socket blocking
SetBlockingMode(ListenSocket, TRUE);
while (true)
{
iRequestNum++;
// Accept incoming connection
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
GetLastError(iResult, L"accept");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// Make the socket non-blocking
SetBlockingMode(ClientSocket, FALSE);
// Receive the data
ReceivedData.clear();
while (true)
{
WSASetLastError(0);
Sleep(5);
// receive data from the client
ZeroMemory(szRecvBuffer, sizeof(szRecvBuffer));
iResult = recv(ClientSocket, szRecvBuffer, sizeof(szRecvBuffer) - 1, 0);
iLastError = WSAGetLastError();
// add received data to the string variable
if (iResult > 0) {
ReceivedData.append(szRecvBuffer);
continue;
}
// connection closed
if (iResult == 0) {
break;
}
// break out of loop if would block
if (iLastError == WSAEWOULDBLOCK) {
break;
}
}