GVKun编程网logo

(死去活来)Delphi IdTCPClient IdTCPServer 点对点传送文件(delphi connector)

16

对于感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解死去活来DelphiIdTCPClientIdTCPServer点对点传送文件,并且为您提供关于Delphi2009,Indy10,TId

对于感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解死去活来Delphi IdTCPClient IdTCPServer 点对点传送文件,并且为您提供关于Delphi 2009,Indy 10,TIdTCPServer.OnExecute,如何获取InputBuffer中的所有字节、Delphi IdTCPClient IdTCPServer 点对点传送文件、delphi idtcpclient和idtcpserver的心跳包、delphi idtcpserver&idtcpclient 演示的宝贵知识。

本文目录一览:

(死去活来)Delphi IdTCPClient IdTCPServer 点对点传送文件(delphi connector)

(死去活来)Delphi IdTCPClient IdTCPServer 点对点传送文件(delphi connector)

总结

以上是小编为你收集整理的(死去活来)Delphi IdTCPClient IdTCPServer 点对点传送文件全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Delphi 2009,Indy 10,TIdTCPServer.OnExecute,如何获取InputBuffer中的所有字节

Delphi 2009,Indy 10,TIdTCPServer.OnExecute,如何获取InputBuffer中的所有字节

我正在搞乱Delphi 2009提供的Indy 10,并且在OnExecute触发时无法从IOHandler获取所有数据……
procedure TFormMain.IdTcpserverExecute(AContext: TIdContext);
var
  RxBufStr: UTF8String;
  RxBufSize: Integer;
begin

  if AContext.Connection.IOHandler.Readable then
  begin
    RxBufSize := AContext.Connection.IOHandler.InputBuffer.Size;
    if RxBufSize > 0 then
    begin
      SetLength(RxBufStr,RxBufSize);
      AContext.Connection.IOHandler.ReadBytes(TBytes(RxBufStr),RxBufSize,False);
    end;
  end;

end;

AContext.Connection.IOHandler.InputBuffer.Size似乎不可靠并经常返回0,但是在下次运行时,OnExecute它将获取正确的字节数,但为时已晚.

基本上我希望能够只获取所有数据,将其填充到UTF8String(不是Unicode字符串),然后解析一个特殊的标记.所以我没有标题,消息长度可变.似乎Indy 10 IOHandlers没有为此设置,或者我只是错误地使用它.

做一些像传递一定大小的缓冲区,尽可能多地填充它并返回实际填充的字节数然后继续运行(如果还有更多)会很好.

除了TIdSchedulerOfFiber的状态之外,这看起来非常有趣,它有用吗?有人用吗?我注意到它不在Delphi 2009的标准安装中.

更新:我发现Msg:= AContext.Connection.IOHandler.ReadLn(#0,enUTF8);哪个有效,但我仍然想知道上述问题的答案,是因为它是基于阻止IO吗?这使得TIdSchedulerOfFiber更加热衷于此.

解决方法

你不应该像这样使用Readable().请尝试以下方法:
procedure TFormMain.IdTcpserverExecute(AContext: TIdContext);
var
  RxBuf: TIdBytes;
begin
  RxBuf := nil;
  with AContext.Connection.IOHandler do
  begin
    CheckForDataOnSource(10);
    if not InputBufferIsEmpty then
    begin
      InputBuffer.ExtractToBytes(RxBuf);
      // process RxBuf as needed...
    end;
  end;
end;

或者:

procedure TFormMain.IdTcpserverExecute(AContext: TIdContext);
var
  RxBufStr: String; // not UTF8String
begin
  with AContext.Connection.IOHandler do
  begin
    CheckForDataOnSource(10);
    if not InputBufferIsEmpty then
    begin
      RxBufStr := InputBuffer.Extract(-1,enUtf8);

      // Alternatively to above,you can set the
      // InputBuffer.Encoding property to enUtf8
      // beforehand,and then call TIdBuffer.Extract()
      // without any parameters.
      //
      // Or,set the IOHandler.DefStringEncoding
      // property to enUtf8 beforehand,and then
      // call TIdioHandler.InputBufferAsstring()

      // process RxBufStr as needed...
    end;
  end;
end;

至于TIdSchedulerOfFiber – 此时SuperCore包实际上已经死了.它还没有在很长一段时间内完成,也没有与最新的Indy 10架构保持同步.我们可能会在以后尝试复活它,但它不在我们不久的将来的计划中.

Delphi IdTCPClient IdTCPServer 点对点传送文件

Delphi IdTCPClient IdTCPServer 点对点传送文件

https://blog.csdn.net/luojianfeng/article/details/53959175

 

2016年12月31日 23:40:15
阅读数:2295

分享图片

Delphi     IdTCPClient 点对点传送文件


客户端向另一个客户端传送文件,不通过服务端中转
那一个很重要的点是,这个客户端也要放一个IdTcpserver,也就是说这个客户端既是客户端,当接收文件的时候也是服务端,必须相应其它客户


端对它的连接,这个时候客户端相当与服务端,好了,明白这个道理就好办了


A客户端(放一个IdTCPClient控件,发送文件)

 

procedure TFormFileSend.FormShow(Sender: TObject);//连接到服务端,同时自己变成服务端
begin

  //自己变成服务端
  IdTcpserver1.Bindings.Clear;
  IdTcpserver1.Bindings.Add.IP:=‘192.168.252.1‘;
  IdTcpserver1.Bindings.Add.Port:=8831;
  IdTcpserver1.Active:=true;
  if  IdTcpserver1.Active then
  begin
    Memo1.Lines.Add(‘服务器已启动‘);
  end
  else
  begin
    Memo1.Lines.Add(‘服务器已停止‘);
  end;

  //连接到服务端
  IdTCPClient1.Host:=FormMain.host;//‘192.168.252.1‘;
  IdTCPClient1.Port:=StrToInt(FormMain.port);//8829;
  if IdTCPClient1.Connected then
    IdTCPClient1.disconnect;
  Try
    IdTCPClient1.Connect;
    IdTCPClient1.WriteLn(FormMain.qm+‘|‘+FormMain.bh);
  except
    MessageBox(Handle,‘服务器没有开启‘,‘提示‘,MB_OK);
    Exit;
  end;


  loading();//连接到服务端,显示上线的客户端
end;


procedure TFormFileSend.loading();
var
  Node: TTreeNode;
begin
  RzCheckTree1.Items.Clear;


  sleep(500);//这里一定要延时,不然下面的数据明明有,但是读不出来, 2016-12-31
  
  with ADOQuery2 do
  begin
    sql.Clear;
    sql.Add(‘select a.ip,a.bh,a.qm,c.qm as bm from ipdz a left join zy b on a.bh=b.bh left join bm c on b.szbm=c.bh ‘);
    Open;
    while not Eof do
    begin
      Node := RzCheckTree1.Items.AddChild(nil,FieldByName(‘qm‘).Asstring+‘(‘+FieldByName(‘bm‘).Asstring+‘)‘+FieldByName(‘ip‘).Asstring);
      Node.Data:=strnew(PChar(FieldByName(‘ip‘).Asstring));
      Next;
    end;
  end;
end;



procedure TFormFileSend.SpeedButton1Click(Sender: TObject);//发送文件
var
  iFileHandle:integer;
  iFileLen,cnt:integer;
  buf:array[0..4096] of byte;


  i: integer;
  zt:Boolean;
begin
  if Edit1.Text=‘‘ then
  begin
    ShowMessage(‘请选择要上传的文件‘);
    Exit;
  end;


  zt:=False;
  for i:=0 to RzCheckTree1.Items.Count - 1 do
  begin
    if RzCheckTree1.ItemState[i] = cschecked then
    begin
      zt:=True;
    end;
  end;
  if zt=False then
  begin
    Application.MessageBox(‘请选择接收人!‘,64);
    exit;
  end;


  for i:=0 to RzCheckTree1.Items.Count - 1 do
  begin
    if RzCheckTree1.ItemState[i] = cschecked then
    begin
      IdTCPClient2.Host:=PChar(RzCheckTree1.Items.Item[i].Data);
      IdTCPClient2.Port:=8831;
      if IdTCPClient2.Connected then
        IdTCPClient2.disconnect;
      Try
        IdTCPClient2.Connect;
      except
        Memo1.Lines.Add(RzCheckTree1.Items.Item[i].Text+‘不在线‘);
        continue;
      end;


      iFileHandle:=FileOpen(Edit1.Text,fmOpenRead);
      iFileLen:=FileSeek(iFileHandle,2);
      FileSeek(iFileHandle,0);
      ProgressBar1.Max:=iFileLen;
      ProgressBar1.Position := 0;
      IdTCPClient2.WriteLn(ExtractFileName(Edit1.Text)+‘|‘+IntToStr(iFileLen));
      while true do
      begin
        Application.ProcessMessages;
        cnt:=FileRead(iFileHandle,buf,4096);
        IdTCPClient2.WriteBuffer(buf,cnt);
        ProgressBar1.Position:=ProgressBar1.Position + cnt;
        Memo1.Lines.Add(‘正在传送文件...‘+DateTimetoStr(Now));
        if cnt<4096 then
          break;
      end;
      FileClose(iFileHandle);
      Memo1.Lines.Add(‘文件传送完成!‘+DateTimetoStr(Now));
    end;
  end;

end;

 

procedure TFormFileSend.SpeedButton5Click(Sender: TObject);//取消发送var  i:Integer;begin  FileClose(iFileHandle);  IdTCPClient2.disconnect;  for i:=0 to RzCheckTree1.Items.Count - 1 do  begin    if RzCheckTree1.ItemState[i] = cschecked then    begin      IdTCPClient2.Host:=PChar(RzCheckTree1.Items.Item[i].Data);      IdTCPClient2.Port:=8831;      if IdTCPClient2.Connected then        IdTCPClient2.disconnect;      Try        IdTCPClient2.Connect;      except        Memo1.Lines.Add(RzCheckTree1.Items.Item[i].Text+‘不在线‘);        continue;      end;      IdTCPClient2.WriteLn(‘取消发送‘);      IdTCPClient2.disconnect;    end;  end;  //Sleep(500);  Memo1.Lines.Add(‘取消文件发送‘+DateTimetoStr(Now));end;

B客户端(要放一个IdTcpserver控件,相当于服务端接收) procedure TFormFileSend.IdTcpserver1Execute(AThread: TIdPeerThread); var   rbyte:array[0..4096] of byte;   sFile:TFileStream;   cmd,FileSize:integer;   str,FileName:string; begin   if not AThread.Terminated and AThread.Connection.Connected then  //注意这里   begin     with AThread.Connection do     begin       Try         str:=AThread.Connection.ReadLn;         if POS(‘|‘,str)>0 then         begin           cmd:=pos(‘|‘,str); //查找分隔符           FileName:=copy(str,1,cmd-1); //提取文件名           FileSize:=StrToInt(copy(str,cmd+1,Length(str)-cmd+1)); //提取文件大小           if MessageBox(0,Pchar(‘您有文件 "‘+FileName+‘" 您是接受还是拒绝?‘),‘文件接受‘,MB_YesNo or MB_ICONQUESTION)=ID_Yes  then //询问是否接收           begin               ProgressBar1.Max:=FileSize div 100;   //初始化进度条               ProgressBar1.Position:=0;               SaveDialog1.FileName:=FileName; //指定保存的默认文件名,一定要在 SaveDialog1.Execute;之前,不然文件名为空               SaveDialog1.Execute;               sFile:=TFileStream.Create(SaveDialog1.FileName,fmCreate); //创建待写入的文件流               While FileSize>4096 do               begin                 Application.ProcessMessages;                 AThread.Connection.ReadBuffer(rbyte,4096);// 读取文件流                 ProgressBar1.Position:=ProgressBar1.Position + (4096 div 100); //更新显示进度                 Memo1.Lines.Add(‘正在接收文件中...‘+DateTimetoStr(Now));                 sFile.Write(rByte,4096);      //写入文件流                 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;

delphi idtcpclient和idtcpserver的心跳包

delphi idtcpclient和idtcpserver的心跳包

总结

以上是小编为你收集整理的delphi idtcpclient和idtcpserver的心跳包全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

delphi idtcpserver&idtcpclient 演示

delphi idtcpserver&idtcpclient 演示

总结

以上是小编为你收集整理的delphi idtcpserver&idtcpclient 演示全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

我们今天的关于死去活来Delphi IdTCPClient IdTCPServer 点对点传送文件的分享已经告一段落,感谢您的关注,如果您想了解更多关于Delphi 2009,Indy 10,TIdTCPServer.OnExecute,如何获取InputBuffer中的所有字节、Delphi IdTCPClient IdTCPServer 点对点传送文件、delphi idtcpclient和idtcpserver的心跳包、delphi idtcpserver&idtcpclient 演示的相关信息,请在本站查询。

本文标签: