調用此函數以在新線程中爲每個客戶端提供服務。在消費函數中,這些檔案被讀取和寫入,但函數在客戶端完成讀取所有響應之前返回,以便套接字超出範圍並關閉,從而在客戶端中創建異常。我假設在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