(死亡历险)Delphi7 IdTCPClient1,IdTCPServer1客户端向服务端发送指令进行文件下载

客户端
procedure TFormFileDownload.SpeedButton8Click(Sender: TObject);
var
  rbyte:array[0..4096] of byte;
  sFile:TFileStream;
  iFileSize:integer;
begin
  IdTCPClient1.Host:=host.Text;//'192.168.252.1';
  IdTCPClient1.Port:=StrToInt(port.Text);//8829;


  if IdTCPClient1.Connected then
    IdTCPClient1.Disconnect;


  Try
    IdTCPClient1.Connect;
  except
    MessageBox(Handle,'服务器没有开启','提示',MB_OK);
    Exit;
  end;
  
  if ADOQuery2.IsEmpty then
  begin
    exit;
  end
  else
  begin
    with IdTCPClient1 do
    begin
      while Connected do
      begin
        try
          SaveDialog1.FileName:=ADOQuery2.FieldByName('filename').AsString; //指定保存的默认文件名,一定要在 SaveDialog1.Execute;之前,不然文件名为空
          //SaveDialog1.Execute;
          if SaveDialog1.Execute then
          begin
            IdTCPClient1.WriteLn(ADOQuery2.FieldByName('filename').AsString+'|'+inttostr(0));
            iFileSize:=IdTCPClient1.ReadInteger;


            ProgressBar1.Max:=ADOQuery2.FieldByName('filesize2').Value div 100;
            ProgressBar1.Position := 0;


            //sFile:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'\接收的文件\'+ADOQuery2.FieldByName('filename').AsString,fmCreate);
            sFile:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
            While iFileSize>4096 do
            begin
              Application.ProcessMessages;


              IdTCPClient1.ReadBuffer(rbyte,4096);


              ProgressBar1.Position:=ProgressBar1.Position + (4096 div 100);
              Memo1.Lines.Add('正在接收文件...'+DateTimeToStr(Now));


              sFile.Write(rByte,4096);
              inc(iFileSize,-4096);
            end;
            IdTCPClient1.ReadBuffer(rbyte,iFileSize);
            sFile.Write(rByte,iFileSize);
            sFile.Free;
            Memo1.Lines.Add('文件接收完成!'+DateTimeToStr(Now));
          end
          else
          begin
            Panel3.Visible:=false;
          end;
        finally
          Disconnect;
        end;
      end;
    end;
  end;
end;
--------------------------------
服务端
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  rbyte:array[0..4096] of byte;
  sFile:TFileStream;
  //cnt:integer;
  cmd,FileSize:integer;
  str,FileName:string;


  iFileHandle:integer;
  iFileLen,cnt:integer;
  buf:array[0..4096] of byte;
begin
  if not AThread.Terminated and AThread.Connection.Connected then  //注意这里
  begin
    with AThread.Connection do
    begin
      Try
        str:=AThread.Connection.ReadLn;   //接收文件大小及文件名
        cmd:=pos('|',str); //查找分隔符
        FileName:=copy(str,1,cmd-1); //提取文件名
        FileSize:=StrToInt(copy(str,cmd+1,Length(str)-cmd+1)); //提取文件大小


        if FileSize=0 then//0表示是客户端发送指令到服务端进行下载文件操作
        begin
          iFileHandle:=FileOpen(ExtractFilePath(ParamStr(0))+'\'+FileName,fmOpenRead);
          iFileLen:=FileSeek(iFileHandle,0,2);
          FileSeek(iFileHandle,0,0);
          AThread.Connection.WriteInteger(iFileLen);
          while true do
          begin
              cnt:=FileRead(iFileHandle,buf,4096);
              AThread.Connection.WriteBuffer(buf,cnt);
              if cnt<4096 then
                  break;
          end;
          FileClose(iFileHandle);
        end
        else
        begin
          //下面是客户端上传文件到服务端
          //if MessageBox(0,Pchar('用户'+AThread.Connection.Socket.Binding.PeerIP+'要给您传送文件 "'+FileName+'" 您是接受还是拒绝?'),'文件接受',MB_YesNo or MB_ICONQUESTION)=ID_Yes then //询问是否接收
          //begin
            //ProgressBar1.Max:=FileSize;   //初始化进度条
            //ProgressBar1.Position:=0;
            //SaveDialog1.FileName:=FileName; //指定保存的默认文件名,一定要在 SaveDialog1.Execute;之前,不然文件名为空
            //SaveDialog1.Execute;
            //sFile:=TFileStream.Create(SaveDialog1.FileName,fmCreate); //创建待写入的文件流
            sFile:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'\'+FileName,fmCreate);
            While FileSize>4096 do
            begin
              Application.ProcessMessages;
              AThread.Connection.ReadBuffer(rbyte,4096);// 读取文件流
              sFile.Write(rByte,4096);      //写入文件流
              //cnt:=AThread.Connection.ReadInteger; //从发送端接收最新的进度位置信息
              //ProgressBar1.Position:=ProgressBar1.Position+cnt; //更新显示进度
              //StatusBar1.Panels[0].Text:='正在接收文件中...';
              Memo1.Lines.Add('正在接收文件中...'+DateTimeToStr(Now));
              inc(FileSize,-4096);
            end;
            AThread.Connection.ReadBuffer(rbyte,FileSize);// .ReadBuffer(rbyte,iLen);
            sFile.Write(rByte,FileSize);
            sFile.Free;
            Memo1.Lines.Add('文件接收完成!'+DateTimeToStr(Now));
          //end;
        end;  
      Finally
        //Disconnect;//断开连接
      end;
    end;
  end;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值