葛歌 發表於 2020-6-23 14:09:00

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

<p><span style="font-size: 16px"><strong>Delphi XE&nbsp; IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)</strong></span></p>
<p><span style="font-size: 16px"><strong>1、IdTCPClient1 端 发送数据</strong></span></p>
<p><span style="font-size: 16px">1.1 发送结构体:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">//定义结构体
TMData = record
      id:Integer;
      Name:Array of Char;
      Age:Byte;
      UpdateTime:double;
end;

//发送
procedure TForm2.Button2Click(Sender: TObject);
var
SendD: TMData;
begin
SendD.ID := 10;</span><br><span style="font-size: 16px">StrPCopy(SendD.Name, 'Delphi 您好');</span><br><span style="font-size: 16px">SendData.age := 18;</span><br><span style="font-size: 16px">SendD.UpdateTime := Now;</span><br><span style="font-size: 16px">IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据
IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));</span><br><span style="font-size: 16px">end;</span></pre>
</div>
<p><span style="font-size: 16px">1.2 发送TStrings类型</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TForm2.Button3Click(Sender: TObject);
var
   sList:TStrings;
   I:Integer;
begin
sList := TStringList.Create;
for I :=0to 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;</span></pre>
</div>
<p><span style="font-size: 16px">1.3 发送一行字符串数据</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TForm2.Button4Click(Sender: TObject);
begin
IdTCPClient1.IOHandler.Write(#10);
IdTCPClient1.IOHandler.Write('Delphi测试',TIdTextEncoding.UTF8);
end;
</span></pre>
</div>
<p><span style="font-size: 16px">  </span></p>
<p><span style="font-size: 16px"><strong>2、IdTCPServer端 接收数据:</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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 :=0to sList.Count-1 dobegin
          Memo1.Lines.Add(sList.Strings);
      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;
</span></pre>
</div>
<p><span style="font-size: 16px">  </span></p>
<p><span style="font-size: 16px">  </span></p>
<p>&nbsp;</p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2020.06.23  更新</span></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/13181802.html
頁: [1]
查看完整版本: Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)