您当前位置:首页 > 新闻频道 > 技术动态 > 正文
indy的IdTcpServer,IdTcpclient传输文件略谈
 

接受部分(有个用户和密码check,你取消就可以)
procedure TTCP_Server.IdTCPServerExecute(AThread: TIdPeerThread);
var
 FStream  : TFileStream;
 FileName  : String;
 CmdStr   : String;
begin
  CmdStr :=AThread.Connection.ReadLn();
  if CompareStr(Copy(CmdStr,1,6),'<SEND>')=0 then
   begin //1
    Delete(CmdStr,1,6);
    Cmd := '上传文件';
    FileName := CmdStr;
    if FileExists(ExtractFilePath(Application.ExeName)+FileName) then
      DeleteFile(ExtractFilePath(Application.ExeName)+FileName);
    try
     FStream := TFileStream.Create(ExtractFilePath(Application.ExeName)+FileName,FmCreate);
     AThread.Connection.ReadStream(FStream,2048,true);
    Finally
     FStream.Free;
     AThread.Connection.Disconnect;
    end;
   end  //1
  else if CompareStr(Copy(CmdStr,1,6),'<AGET>')=0 then
   begin //2
    Delete(CmdStr,1,6);
    Cmd := '下载文件';
    FileName := CmdStr;
    if FileExists(ExtractFilePath(Application.ExeName)+FileName) then
      begin
       try
        FStream := TFileStream.Create(ExtractFilePath(Application.ExeName)+FileName,FmOpenRead);
        AThread.Connection.WriteStream(FStream,True,False);
       Finally
        FStream.Free;
        AThread.Connection.Disconnect;
       end;
      end
    else
      begin
       AThread.Connection.WriteLn('Failed');
       AThread.Connection.Disconnect;
      end;
   end //2
  else if CompareStr(Copy(CmdStr,1,6),'<USER>')=0 then
   begin //3
    Delete(CmdStr,1,6);
    User := UpperCase(Trim(DecryptString(CmdStr,20504))); //CmdStr;
    if CompareStr(User,'XDM')=0 then
      begin
       Memo1.Lines.Add('User: '+User);
       CmdStr :='';
      end
    else
      begin
       Memo1.Lines.Add('User: '+User+' 非法登陆,已关闭连接');
       CmdStr := '';
       AThread.Connection.WriteLn('<Failed>');
       AThread.Connection.Disconnect;
      end;
   end  //3
  else if CompareStr(Copy(CmdStr,1,6),'<PASS>')=0 then
   begin //4


    Delete(CmdStr,1,6);
    Password := DecryptString(CmdStr,20504); //CmdStr;
    if CompareStr(Password,'123abc')=0 then
      begin
       Memo1.Lines.Add('Login..........Accepted');
       Memo1.Lines.Add('Connected......'+DateTimeToStr(Now));
       CmdStr := '';
       AThread.Connection.WriteLn('<Accepted>');
      end
    else
      begin
       Memo1.Lines.Add('Password Wrong...Login Failure');
       AThread.Connection.WriteLn('<Failed>');
       CmdStr :='';
       AThread.Connection.Disconnect;
      end;
   end;  //4
end;

主要发送部分
procedure TTCP_Client.BtnSendClick(Sender: TObject);
var
 FStream : TFileStream;
begin
 if IdTcpClient.Connected then begin
   IdTcpClient.WriteLn('<SEND>'+ExtractFileName(FileName));
   if FileName = '' then
    MessageBox(Handle,'没有选择文件','Error',MB_OK)
   else begin
      try
      FStream := TFileStream.Create(FileName,FmOpenRead);
      FStream.Position := 0;
      FStream.Seek(0,0);
      IdTcpClient.WriteStream(FStream,true,false);
      Finally
      FStream.Free;
      IdTcpClient.Disconnect;
      end;
      Memo1.Lines.Add('Transferred OK');
     end;
 end
 else
  begin
   MessageBox(Handle,'没有连接服务器','Error',MB_Ok);
  end;
end;

关键字:通讯
About Us - 关于我们 - 服务列表 - 付费指导 - 媒体合作 - 广告服务 - 版权声明 - 联系我们 - 网站地图 - 常见问题 - 友情链接
Copyright©2014安装信息网 www.zgazxxw.com. All rights reserved.
服务热线:4000-293-296 联系电话:0371-61311617 传真:0371-55611201 QQ: 邮箱:zgazxxw@126.com 豫ICP备18030500号-4
未经过本站允许,请勿将本站内容传播或复制
安全联盟认证