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

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

1、IdTCPClient1 端 发送数据

1.1 发送结构体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//定义结构体
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类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 发送一行字符串数据

1
2
3
4
5
procedure TForm2.Button4Click(Sender: TObject);
begin
  IdTCPClient1.IOHandler.Write(#10);
  IdTCPClient1.IOHandler.Write('Delphi测试',TIdTextEncoding.UTF8); 
end;

  

2、IdTCPServer端 接收数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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  更新

 

posted on   滔Roy  阅读(2729)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报

导航

点击右上角即可分享
微信分享提示