求救关于线程阻塞模式下的TServerSocket控件的使用问题
程序实现目标:
创建一个服务器,监听网络中的请求,如果收到请求,就发送数据给请求方
主要控件: TServerSocket
控件TServerSocket的属性ServerType为stThreadBlocking
主要代码如下:
线程类头文件:
class PACKAGE TMyServerThread : public Scktcomp::TServerClientThread
{
public:
// if true, FreeOnTerminate is set to false before the thread terminates,
// and the thread is left in the thread cache. When KeepInCache is false,
// the thread is freed when execution terminates.
__fastcall TMyServerThread(bool CreateSuspended, TServerClientWinSocket* ASocket)
: Scktcomp::TServerClientThread(CreateSuspended, ASocket)
{ CreateSuspended = false; KeepInCache=true; FreeOnTerminate=false; };
// To implement this thread, you override the ClientExecute method instead of the
// Execute method.
void __fastcall ClientExecute(void);
private:
TWinSocketStream *pStream;
long downloadALLPre(bool isAll);
void downloadAll(bool isAll = true);
void uploadAll(bool isALl = true);
};
线程类的CPP文件
void __fastcall TMyServerThread::ClientExecute(void)
{
Synchronize(DownLoad);
}
void __fastcall TMyServerThread::DownLoad(void)
{
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
// Now, use TWinSocketStream to read or write information
// over a blocking socket connection
int nResvLen = 0;
AnsiString temp = "";
pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
try
{
//char pro_buf[PRO_PACKAGESIZE];
memset( pro_buf, 0, sizeof(pro_buf) );
// give the client 60 seconds to start writing
if (pStream->WaitForData(CLIENTWAITTIME))
{
nResvLen = pStream->Read(pro_buf,sizeof(pro_buf));
if (nResvLen == 0)
// (if can't read in 60 seconds) than close the connection
ClientSocket->Close();
else
{
// Client to Server test text
unassemblePackage(pro_buf, nResvLen);
if(strncmp(resvPack.header, sentPack.header, sizeof sentPack.header) == 0)
{
if(resvPack.order1 == sentPack.order1)
{
temp.sprintf("%8s", resvPack.machineid);
temp.SetLength(8);
if( 1 <= resvPack.order2 && resvPack.order2 <= 4)
{
DataModule1->curMachineID = temp;
sck_init(DataModule1->curMachineID.c_str());
Form1->Memo->Lines->Add("机器" + DataModule1->curMachineID + "加入连
接");
DataModule1->state = resvPack.order2;
}
switch(DataModule1->state)
{
case PRO_UPLOAD_PART:
uploadAll(false);
break;
case PRO_UPLOAD_ALL:
uploadAll();
break;
case PRO_DOWNLOAD_PART:
downloadAll(false);
break;
case PRO_DOWNLOAD_ALL:
downloadAll();
break;
}
}
else
{
Form1->Memo->Lines->Add("收到非正常信息帧,忽略");
nPackLen = assemblePackage(pro_buf, PRO_PACKAGESIZE, PRO_COMPELTE, NULL,0, NULL);
pStream->Write( pro_buf, sizeof(pro_buf));
DataModule1->init();
}
}
else
{
Form1->Memo->Lines->Add("收到非正常信息帧,忽略");
nPackLen = assemblePackage(pro_buf, PRO_PACKAGESIZE, PRO_COMPELTE, NULL,0, NULL);
pStream->Write( pro_buf, sizeof(pro_buf));
DataModule1->init();
}
}
}
else
ClientSocket->Close();
}
__finally
{
delete pStream;
}
}
catch (...)
{
HandleException();
}
}
}
TServerSocket采用stThreadBlocking模式,控件的OnGetThread事件代码如下:
void __fastcall TDataModule1::ServerSocketGetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
SocketThread = new TMyServerThread(false, ClientSocket);
SocketThread->WaitFor();
SocketThread->Free();
}
问题:
当局域网中只有一台机器发来了下载请求时服务器程序运行正常,而当同时有两台机器下载的时候,服务器对其中的一台的请求在收到请求数据帧后却不给以相应的应答ACK数据帧,造成两台中有一台不能下载数据.
请大侠们全力指导,问题比较急