Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)

Posted guorongtao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)相关的知识,希望对你有一定的参考价值。

Delphi XE  IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)

1、IdTCPClient1 端 发送数据

1.1 发送结构体:

//定义结构体
TMData = record
      id:Integer;
      Name:Array[0..20] of Char;
      Age:Byte;
      UpdateTime:double;
  end;

//发送
procedure TForm2.Button2Click(Sender: TObject);
var
  SendD: TMData;
begin
  SendD.ID := 10;
StrPCopy(SendD.Name, ‘Delphi 您好‘);
SendData.age := 18;
SendD.UpdateTime := Now;
IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据 IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));
end;

1.2 发送TStrings类型

procedure TForm2.Button3Click(Sender: TObject);
var
   sList:TStrings;
   I:Integer;
begin
  sList := TStringList.Create;
  for I :=0  to 50 do
  begin
    sList.Add(‘数据Test‘ + IntToStr(i));
  end;
  IdTCPClient1.IOHandler.Write(#200);  
  IdTCPClient1.IOHandler.Write(sList.Count);
  IdTCPClient1.IOHandler.Write(ToBytes(sList.Text,TIdTextEncoding.UTF8));
end;

1.3 发送一行字符串数据

procedure TForm2.Button4Click(Sender: TObject);
begin
  IdTCPClient1.IOHandler.Write(#10);
  IdTCPClient1.IOHandler.Write(‘Delphi测试‘,TIdTextEncoding.UTF8);  
end;

  

2、IdTCPServer端 接收数据:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
   RData:TMData;
   buf:TIdBytes;
   sCmd:Char;
   sList:TStrings;
   I:Integer;
   ListCount:Integer;
begin
   sCmd := AContext.Connection.IOHandler.ReadChar;  //先读取Char结构数据
   if sCmd = #100 then  //接收结构体
   begin
     AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));
     BytesToRaw(buf, RData, SizeOf(RData));
     with Memo1.lines do begin
       Add(‘ID:‘+Inttostr(RData.Id));
       Add(‘Name:‘+StrPas(RData.Name));
       Add(‘Age:‘+Inttostr(ReadData.age));
       Add(‘UpdateTime:‘+DateTimeToStr(RData.UpdateTime));
     end;
   end else if sCmd = #200 then  //接收 TStrings
   begin
      ListCount := AContext.Connection.IOHandler.ReadLongInt;  //ReadLongInt
      sList := TStringList.Create;
      try
        AContext.Connection.IOHandler.ReadStrings(sList,ListCount,TIdTextEncoding.UTF8);
        for I :=0  to sList.Count-1 do  begin
          Memo1.Lines.Add(sList.Strings[I]);
        end;
      finally
        sList.Free;
      end;
   end else if sCmd = #10 then
   begin
     Memo1.Lines.Add(AContext.Connection.IOHandler.ReadString(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8) );
   end else
     AContext.Connection.IOHandler.InputBuffer.Clear;  //清除
end;

  

  

 

创建时间:2020.06.23  更新

 

 

 

procedure TForm2.Button2Click(Sender: TObject);
var
  SendData:TMyData;
begin
SendData.ID:=10;
StrPCopy(SendData.Name,‘Wyatt‘);
StrPCopy(SendData.Sex,‘男‘);
SendData.age:=25;
StrPCopy(SendData.Address,‘江苏省‘);
SendData.UpdateTime:=Now;
IdTCPClient1.IOHandler.Write(#99);//接收时便于区分接收的数据类型 自定义
IdTCPClient1.IOHandler.Write(RawToBytes(SendData,SizeOf(SendData)));

end;

以上是关于Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)的主要内容,如果未能解决你的问题,请参考以下文章

Delphi XE8 中TIdTCPClient的WriteLn编码变化!

Delphi XE8报错:There is no overloaded version of 'ReadLn' that can be called with these argume

delphi indy10 无法接收中文

delphi xe 取CPU等硬件信息

Delphi XE8中Delphi和JAVA数据类型对应关系!

delphi xe10 怎么样