大家帮帮忙,关于阻塞式多线程的Socket连接
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ClientThread.h"
以下是服务端的线程执行程序
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
using namespace log4cplus;
DECLEAR_SUBLOGITEM(TClientThread);
void __fastcall TClientThread::ClientExecute(void)
{
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
// Now, use TWinSocketStream to read or write information
// over a blocking socket connection
TWinSocketStream *pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
try
{
char ReceiveBuffer[RECEIVESIZE];
memset( ReceiveBuffer, 0, sizeof(ReceiveBuffer) );
char SendBuffer[SENDSIZE];
memset( SendBuffer, 0, sizeof(SendBuffer));
// give the client 60 seconds to start writing
if(pStream->WaitForData(CLIENTWAITTIME))
{
if (pStream->Read(ReceiveBuffer, sizeof(ReceiveBuffer)) == 0)
// (if can't read in 60 seconds) than close the connection
{
ClientSocket->Close();
}
else
{
//业务操作
} //else
}
}
__finally
{
delete pStream;
}
}
catch (...)
{
HandleException();
return;
}
}
}
//------------------------------------------------------------------------------
目前的问题就是,当客户端关闭,服务端继续执行“pStream->WaitForData(CLIENTWAITTIME))”。
此时就会抛出“网络名已不可用”的异常,执行到catch,然后又跳到pStream->WaitForData
然后就会抛出内存引用的异常。
大家帮帮忙啊,谢谢了!!!!!!