tidtcp控件传送文件,实现一个客户发送到另一个客户,为什么不能发两次,请看代码

jackyjian 2005-05-21 05:38:47
发送过程是这样的:首先客户端点击发送文件按钮,发送到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;
}
}
}

第一次点击发送按钮,发送成功,再点击,程序没反应,请高手不吝赐教.
...全文
126 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
constantine 2005-08-10
  • 打赏
  • 举报
回复
解决就好
我不懂电脑 2005-08-10
  • 打赏
  • 举报
回复
祝贺
sfengnet 2005-08-10
  • 打赏
  • 举报
回复
靠人不如靠己
jackyjian 2005-05-23
  • 打赏
  • 举报
回复
问题解决了.

1,317

社区成员

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

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