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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/luojianfeng/article/details/53888069
客户端
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;
展开阅读全文

没有更多推荐了,返回首页