tidtcp控件传送文件,实现一个客户发送到另一个客户,为什么不能发两次,请看代码
发送过程是这样的:首先客户端点击发送文件按钮,发送到server。
void __fastcall TFormclient::SendfileClick(TObject *Sender)
{
String Filepathname,Filename,Filemsg,Toname;
TFileStream *Stream;
Toname=Listclients->Items->Strings[Listclients->ItemIndex];// 从客户列表里选择发送给的客户
if(Od->Execute())
{
Filepathname=Od->FileName;
Filename=ExtractFileName(Filepathname);
Stream=new TFileStream(Filepathname,fmOpenRead);
Client->WriteLn("@FILE"); //发送文件协议
Client->WriteLn(Fromname);
Client->WriteLn(Toname);
Client->WriteLn(Filename);
Client->WriteStream(Stream,-1,true);
delete Stream;
}
}
然后:server端收到文件后转发:
void __fastcall Tserverform::ServerExecute(TIdPeerThread *AThread)
{
String Msg;
TMemoryStream * Stream=new TMemoryStream();
try{
Msg=AThread->Connection->ReadLn();
}
catch(...)
{
}
if(Msg=="@FILE")
{
int i;
TIdPeerThread *Thread;
TClient * Client;
String Fromname=AThread->Connection->ReadLn();
String Toname=AThread->Connection->ReadLn();
String Filename=AThread->Connection->ReadLn();
AThread->Connection->ReadStream(Stream,-1,false);
Stream->Seek(0,soFromBeginning);
for(i=0;i<Clients->Count;i++) //从client List里找接收客户线程
{
Client=(TClient*)Clients->Items[i];
if(Client->Name==Toname)
{
Thread=(TIdPeerThread *)Client->Thread; //找到线程
break;
}
}
delete Client;
if(i<Clients->Count)
{
Thread->Connection->WriteLn("@FILE"); //协议
Thread->Connection->WriteLn(Fromname);
Thread->Connection->WriteLn(Filename);
Thread->Connection->WriteStream(Stream,-1,true); //转发
}
delete Stream;
}
}
接收客户接收:
void __fastcall TFormclient::TimerTimer(TObject *Sender) //timer 事件
{
static int errorctr=0;
String Msg;
if(!Client->Connected())
return;
try
{
Msg = Client->ReadLn("",5);
}
catch (...)
{
errorctr++;
if (errorctr>4)
{Timer->Enabled=false;
ShowMessage("Error Recieving Message");
return;
}
}
if(Msg=="@FILE") //符合协议,接收
{
String Fromname,Toname,Filename;
TMemoryStream *Stream=new TMemoryStream();
Fromname=Client->ReadLn();
Filename=Client->ReadLn();
Memo->Lines->Add("come from "+Fromname+" file: "+Filename);
Client->ReadStream(Stream,-1,false); //接收
Stream->Seek(0,soFromBeginning);
Stream->SaveToFile(ExtractFilePath(ParamStr(0))+"\\temp\\"+Filename);
delete Stream;
}
}
}
第一次点击发送按钮,发送成功,再点击,程序没反应,请高手不吝赐教.