调用此函数以在新线程中为每个客户端提供服务。在消费函数中,这些档案被读取和写入,但函数在客户端完成读取所有响应之前返回,以便套接字超出范围并关闭,从而在客户端中创建异常。我假设在CArchive上的任何写入都应该阻塞,直到在客户端读取它为止。我在这里做出错误的假设吗? 如果我在超出范围(尝试)之前添加延迟(这不是一种好方法),代码工作正常,我不知道在阻止所有数据传输之前是否有任何方法阻止?CSocket在发送时不会阻塞
感谢
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;
}
这是一个非常繁忙的服务器。我不能长时间保持插座,所以你的解决方案不适用于我的情况。 – Mehran 2009-12-04 15:29:54