This function is called to serve each client in a new thread. in the consume function, these archives are read and written to but the function returns before the client finishes reading all the response so the socket goes out of scope and closed, creating an exception in client. I'm assuming that any write on the CArchive should block until its read on the client side. Am I making a wrong assumption here? The code works fine if I add a delay before going out of scope ( try ) which is not a good way, I wonder is there any way to block until all the data is transferred?
Thanks
UINT CNetServer::serveClient(LPVOID p)
{
serveClientParams* params = reinterpret_cast<serveClientParams*>(p);
try
{
AfxSocketInit();
CSocket clientSocket;
clientSocket.Attach(params->ClientSocket);
CSocketFile file(&clientSocket);
CArchive arIn (&file, CArchive::load);
CArchive arOut(&file, CArchive::store);
params->ServerInstance->Consumer.Consume(arIn, arOut);
arOut.Flush();
file.Flush();
//SleepEx(1000,true); works fine is I wait till the data is sent.
}
catch(int ex)
{
CMisc::LogWriteWarning(ex, GetLastError(), "Listen Loop Communication");
}
catch(CException* ex)
{
char buffer[1024];
ex->GetErrorMessage(buffer, sizeof(buffer));
CMisc::LogWriteError(buffer, SOCKET_COMUNICATION_FAILED);
}
catch(...)
{
CMisc::LogWriteWarning(0, GetLastError(), "abnormal communication termination.");
}
delete params;
return 0;
}