Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)
2021-01-05 09:27
标签:list adb 字符串 cti buffer time lis timetostr write Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10) 1、IdTCPClient1 端 发送数据 1.1 发送结构体: 1.2 发送TStrings类型 1.3 发送一行字符串数据 2、IdTCPServer端 接收数据: 创建时间:2020.06.23 更新 procedure TForm2.Button2Click(Sender: TObject); Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10) 标签:list adb 字符串 cti buffer time lis timetostr write 原文地址:https://www.cnblogs.com/guorongtao/p/13181802.html//定义结构体
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;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;
procedure TForm2.Button4Click(Sender: TObject);
begin
IdTCPClient1.IOHandler.Write(#10);
IdTCPClient1.IOHandler.Write(‘Delphi测试‘,TIdTextEncoding.UTF8);
end;
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;
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)
文章链接:http://soscw.com/index.php/essay/40230.html