阻塞方式的话,最好用多线程处理。死掉的原因可能是因为判断tcp错误需要一定的时间。
下面是一个用TWinSocketStream的例子:
The following example shows a typical ClientExecute method for a custom descendant of TServerClientThread. It reads from the socket connection specified by ClientSocket using a thread-local instance of TWinSocketStream.
{
TWinSocketStream *pStream;
char Buffer[10];
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
Stream = new TWinSocketStream(ClientSocket, 60000);
try
{
memset(Buffer, 0, 10); // initialize the buffer
// give the client 60 seconds to start writing
if (pStream->WaitForData(60000))
{
if pStream->Read(Buffer, 10) == 0) // if can抰 read in 60 seconds
ClientSocket->Close(); // close the connection
// now process the request
...
}
else
ClientSocket->Close(); // if client doesn抰 start, close
}
__finally
{
delete pStream;
}
}
catch (Exception &E)
{
HandleException();
}
}
}