大家帮帮忙,关于阻塞式多线程的Socket连接

jinxintang 2006-07-05 06:34:44
//---------------------------------------------------------------------------
#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
然后就会抛出内存引用的异常。
大家帮帮忙啊,谢谢了!!!!!!
...全文
342 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jadeluo 2006-07-10
  • 打赏
  • 举报
回复
// 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
{
ClientSocket->Close();
}
痞子酷 2006-07-10
  • 打赏
  • 举报
回复
if (pStream->Read(ReceiveBuffer, sizeof(ReceiveBuffer)-1) == 0)
jishiping 2006-07-06
  • 打赏
  • 举报
回复
HandleException(); 是怎么写的?
阻塞式Socket,遇到错误就结束通讯,不要再做其它事情了。

这个用Codeguard,应该可以查到错在哪儿。

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧