羊奶 發表於 2021-5-13 17:26:00

Delphi中Indy10的IdTcpClient和IdTcpServer数据通信

<p>---------例子是XE的----</p>
<p>只是本人测试的,仅供学习!代码很乱!</p>
<p>&nbsp;</p>
<p>在一个程序中测试&nbsp;IdTcpClient和IdTcpServer有点坑,</p>
<p>测试的时候最好写Client和Server两个程序测试</p>
<p>&nbsp;</p>
<p>--------------------</p>
<p>&nbsp;</p>
<p>-------------------------Unit开始</p>
<p>unit Unit1;</p>
<p>interface</p>
<p>uses<br>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>Dialogs, IdContext, StdCtrls, IdTCPConnection, IdTCPClient, IdBaseComponent,<br>IdComponent, IdCustomTCPServer, IdTCPServer,IdGlobal, IdIOHandler,<br>IdIOHandlerStream, IdIntercept, IdIOHandlerSocket, IdIOHandlerStack, ExtCtrls,<br>IdAntiFreezeBase, IdAntiFreeze;</p>
<p>type<br>TMData = record<br>      id:Integer;<br>      Name:Array of Char;<br>      Age:Byte;<br>      UpdateTime:double;<br>end;<br>TForm1 = class(TForm)<br>    IdTCPServer1: TIdTCPServer;<br>    Button1: TButton;<br>    Button2: TButton;<br>    Button3: TButton;<br>    Memo1: TMemo;<br>    IdTCPClient1: TIdTCPClient;<br>    Button4: TButton;<br>    Button5: TButton;<br>    Timer1: TTimer;<br>    Edit1: TEdit;<br>    Button6: TButton;<br>    IdAntiFreeze1: TIdAntiFreeze;<br>    Edit2: TEdit;<br>    procedure Button1Click(Sender: TObject);<br>    procedure IdTCPServer1Execute(AContext: TIdContext);<br>    procedure FormShow(Sender: TObject);<br>    procedure FormDestroy(Sender: TObject);<br>    procedure IdTCPServer1Connect(AContext: TIdContext);<br>    procedure Button5Click(Sender: TObject);<br>    procedure Button4Click(Sender: TObject);<br>    procedure Timer1Timer(Sender: TObject);<br>    procedure IdTCPClient1Connected(Sender: TObject);<br>    procedure Button6Click(Sender: TObject);<br>private<br>    ClientIPList:TList;<br>    { Private declarations }<br>public<br>    { Public declarations }<br>end;</p>
<p>var<br>Form1: TForm1;</p>
<p>implementation<br>uses IdSchedulerOfThread;<br>{$R *.dfm}</p>
<p>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>SendD: TMData;<br>begin<br>if not IdTCPClient1.Connectedthen<br>    IdTCPClient1.Connect;<br>SendD.ID := 10;<br>StrPCopy(SendD.Name,'Delphi 您好');<br>SendD.age := 18;<br>SendD.UpdateTime := Now;<br>IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;<br>IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据<br>IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));</p>
<p>end;</p>
<p>procedure TForm1.Button4Click(Sender: TObject);<br>var<br>MyIdTCPConnection:TIdTCPConnection;<br>vIOHandler:TIdIOHandler;<br>vv:TIdContext;<br>begin<br>try<br>    //MyIdTCPConnection:=TIdTCPConnection.Create(nil);<br>    //vIOHandler:=TIdIOHandler.Create(nil);<br>    //MyIdTCPConnection.IOHandler :=vIOHandler;<br>    //<br>    //ShowMessage(TIdContext(ClientIPList.Items).Connection.Name+','+TIdContext(ClientIPList.Items).binding.peerIp);</p>
<p>    //ShowMessage(IntToStr(TIdTCPConnection(ClientIPList.Items).IOHandler.Port)+',,'+TIdTCPConnection(ClientIPList.Items).IOHandler.Host);<br>    //MyIdTCPConnection.IOHandler.Port:=5050;<br>    //MyIdTCPConnection.IOHandler.Host:='192.168.1.50';<br>    //MyIdTCPConnection.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;<br>    //MyIdTCPConnection.Socket.Binding.SetPeer('192.168.1.50',5050);<br>    //MyIdTCPConnection.Socket.Open ;<br>    //MyIdTCPConnection.IOHandler.Open;<br>    //MyIdTCPConnection.IOHandler.Write('Qaaam');<br>    //ShowMessage(TIdTCPConnection(ClientIPList.Items));<br>    //TIdContext(ClientIPList.Items).Connection.IOHandler.Write('AAAA');<br>    //TIdTCPConnection(ClientIPList.Items).IOHandler.Write(#10,TIdTextEncoding.ASCII);<br>    //TIdTCPConnection(ClientIPList.Items).IOHandler.Writeln('QAAAAN',TIdTextEncoding.ASCII);<br>    //----------------------------<br>    //ShowMessage(IntToStr(TIdYarnOfThread(TIdContext(ClientIPList.Items).Yarn).Thread.ThreadID));<br>    //if TIdYarnOfThread(TIdContext(ClientIPList.Items).Yarn).Thread.Suspendedthen<br>      //TIdYarnOfThread(TIdContext(ClientIPList.Items).Yarn).Thread.Resume;<br>      ////vv:=TIdContext(ClientIPList.Items);<br>    //TIdContext(ClientIPList.Items).Connection.IOHandler.CheckForDisconnect(True, True);<br>    ////vv.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.ASCII;<br>    //TIdContext(ClientIPList.Items).Connection.IOHandler.Write('QAAAAN',TIdTextEncoding.ASCII);<br>    ////ifvv.Connection.IOHandler.Openedthen<br>      ////ShowMessage('已连接');<br>      ////vv.Connection.Socket.Write('200: 数据接收成功!');<br>    //啊啊啊啊啊啊啊啊,气死人了!测试了这么多,以为代码有问题!!!!在同一个进程中这玩意有问题,<br>    //建立客户端服务器两个程序的时候,这样的代码可以!妈耶!不知道什么情况!!!<br>    vv:=TIdContext(IdTCPServer1.Contexts.LockList.Items);<br>    vv.Connection.IOHandler.Write('QAAAAN');</p>
<p>    //------------------------------<br>    //TIdTCPConnection(ClientIPList.Items).SendCmd('888') ;<br>finally<br>    //FreeAndNil(MyIdTCPConnection);<br>    //FreeAndNil(vIOHandler);<br>    IdTCPServer1.Contexts.UnlockList;<br>end;<br>end;</p>
<p>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br>Timer1.Enabled:=not Timer1.Enabled ;<br>if Timer1.Enabled then<br>    Button5.Caption:='不显示'<br>else<br>    Button5.Caption:='显示';<br>//ShowMessage(IdTCPClient1.Intercept.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8 ));<br>//ShowMessage(IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,) );<br>end;</p>
<p>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>//IdTCPClient1.IOHandler.Write(#10);<br>if not IdTCPClient1.Connectedthen<br>    IdTCPClient1.Connect;<br>IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;<br>//IdTCPClient1.IOHandler.Write(#0); //提前发送一个标识符,用于区分数据<br>IdTCPClient1.IOHandler.Write('BAAAAC',TIdTextEncoding.ASCII);<br>end;</p>
<p>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>FreeAndNil(ClientIPList);<br>end;</p>
<p>procedure TForm1.FormShow(Sender: TObject);<br>begin<br>ClientIPList:=TList.Create;<br>IdTCPServer1.Active:=True;</p>
<p>end;</p>
<p>procedure TForm1.IdTCPClient1Connected(Sender: TObject);<br>begin<br>//ShowMessage('连接客户端');<br>end;</p>
<p>procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);<br>begin</p>
<p>if Length(AContext.Binding.PeerIP)&gt;5 then//AContext.Connection.Socket.Binding.PeerIP;<br>    ClientIPList.Add(AContext);<br>    //TIdYarnOfThread(acontext.Yarn).Thread.ThreadID;</p>
<p>end;</p>
<p>procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);<br>var<br>   RData:TMData;<br>   buf:TIdBytes;<br>   sCmd:Char;<br>   sList:TStrings;<br>   I:Integer;<br>   ListCount:Integer;<br>   ss:string;<br>begin<br>//ACONTEXT.Connection.IOHandler.WriteLn('Recive Message!');<br>//AContext.Connection<br>{sCmd := AContext.Connection.IOHandler.ReadChar;//先读取Char结构数据<br>if sCmd = #100 then//接收结构体<br>begin<br>   AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));<br>   BytesToRaw(buf, RData, SizeOf(RData));<br>   with Memo1.lines do begin<br>       Add('IP:'+AContext.Connection.Socket.Binding.PeerIP);<br>       Add('ID:'+Inttostr(RData.Id));<br>       Add('Name:'+StrPas(RData.Name));<br>       Add('Age:'+Inttostr(RData.age));<br>       Add('UpdateTime:'+DateTimeToStr(RData.UpdateTime));<br>   end;<br>end                                 //TCPClient.IOHandler.DefStringEncoding:=TEncoding.ASCII;<br>else }<br>//sCmd := AContext.Connection.IOHandler.ReadChar ;<br>ss:=AContext.Connection.IOHandler.Readstring(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.ASCII );<br>if ss&lt;&gt;'' then<br>begin<br>    Memo1.Lines.Add(ss);   //AContext.Connection.IOHandler.InputBuffer.Size,<br>    AContext.Connection.IOHandler.Write('88888888888',TIdTextEncoding.ASCII );<br>end;</p>
<p><br>end;</p>
<p>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> ss:string;<br>begin<br>ss:=IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size);<br>Edit2.Text:=ss;<br>if ss&lt;&gt;'' then<br>    Edit1.Text:=ss</p>
<p><br>//IdTCPClient1.IOHandler.InputBuffer.Clear;<br>end;</p>
<p>end.</p>
<p>--------------------------------------<br>Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)_136.lahttps://www.136.la/shida/show-131002.html<br>-------------------------------------------------------------------------------------</p>
<p>1、IdTCPClient1 端 发送数据</p>
<p>1.1 发送结构体:</p>
<p>//定义结构体<br>TMData = record<br>      id:Integer;<br>      Name:Array of Char;<br>      Age:Byte;<br>      UpdateTime:double;<br>end;</p>
<p>//发送<br>procedure TForm2.Button2Click(Sender: TObject);<br>var<br>SendD: TMData;<br>begin<br>SendD.ID := 10;<br>StrPCopy(SendD.Name, ‘Delphi 您好‘);<br>SendData.age := 18;<br>SendD.UpdateTime := Now;<br>IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据<br>IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));<br>end;<br>1.2 发送TStrings类型</p>
<p>procedure TForm2.Button3Click(Sender: TObject);<br>var<br>   sList:TStrings;<br>   I:Integer;<br>begin<br>sList := TStringList.Create;<br>for I :=0to 50 do<br>begin<br>    sList.Add(‘数据Test‘ + IntToStr(i));<br>end;<br>IdTCPClient1.IOHandler.Write(#200);  <br>IdTCPClient1.IOHandler.Write(sList.Count);<br>IdTCPClient1.IOHandler.Write(ToBytes(sList.Text,TIdTextEncoding.UTF8));<br>end;<br>1.3 发送一行字符串数据</p>
<p>procedure TForm2.Button4Click(Sender: TObject);<br>begin<br>IdTCPClient1.IOHandler.Write(#10);<br>IdTCPClient1.IOHandler.Write(‘Delphi测试‘,TIdTextEncoding.UTF8);<br>end;<br>  </p>
<p>2、IdTCPServer端 接收数据:</p>
<p>procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);<br>var<br>   RData:TMData;<br>   buf:TIdBytes;<br>   sCmd:Char;<br>   sList:TStrings;<br>   I:Integer;<br>   ListCount:Integer;<br>begin<br>   sCmd := AContext.Connection.IOHandler.ReadChar;//先读取Char结构数据<br>   if sCmd = #100 then//接收结构体<br>   begin<br>   AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));<br>   BytesToRaw(buf, RData, SizeOf(RData));<br>   with Memo1.lines do begin<br>       Add(‘ID:‘+Inttostr(RData.Id));<br>       Add(‘Name:‘+StrPas(RData.Name));<br>       Add(‘Age:‘+Inttostr(ReadData.age));<br>       Add(‘UpdateTime:‘+DateTimeToStr(RData.UpdateTime));<br>   end;<br>   end else if sCmd = #200 then//接收 TStrings<br>   begin<br>      ListCount := AContext.Connection.IOHandler.ReadLongInt;//ReadLongInt<br>      sList := TStringList.Create;<br>      try<br>      AContext.Connection.IOHandler.ReadStrings(sList,ListCount,TIdTextEncoding.UTF8);<br>      for I :=0to sList.Count-1 dobegin<br>          Memo1.Lines.Add(sList.Strings);<br>      end;<br>      finally<br>      sList.Free;<br>      end;<br>   end else if sCmd = #10 then<br>   begin<br>   Memo1.Lines.Add(AContext.Connection.IOHandler.ReadString(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8) );<br>   end else<br>   AContext.Connection.IOHandler.InputBuffer.Clear;  //清除<br>end;</p>
<p>&nbsp;</p>
<p>--------------------------Unit结束</p>
<p>&nbsp;</p>
<p>----------------Form开始</p>
<p>object Form1: TForm1<br>Left = 0<br>Top = 0<br>Caption = 'Form1'<br>ClientHeight = 383<br>ClientWidth = 870<br>Color = clBtnFace<br>Font.Charset = DEFAULT_CHARSET<br>Font.Color = clWindowText<br>Font.Height = -11<br>Font.Name = 'Tahoma'<br>Font.Style = []<br>OldCreateOrder = False<br>OnDestroy = FormDestroy<br>OnShow = FormShow<br>PixelsPerInch = 96<br>TextHeight = 13<br>object Button1: TButton<br>    Left = 32<br>    Top = 272<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button1'<br>    TabOrder = 0<br>    OnClick = Button1Click<br>end<br>object Button2: TButton<br>    Left = 184<br>    Top = 296<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button2'<br>    TabOrder = 1<br>end<br>object Button3: TButton<br>    Left = 312<br>    Top = 296<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button3'<br>    TabOrder = 2<br>end<br>object Memo1: TMemo<br>    Left = 536<br>    Top = 8<br>    Width = 313<br>    Height = 313<br>    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861<br>    Lines.Strings = (<br>      'Memo1')<br>    TabOrder = 3<br>end<br>object Button4: TButton<br>    Left = 536<br>    Top = 350<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button4'<br>    TabOrder = 4<br>    OnClick = Button4Click<br>end<br>object Button5: TButton<br>    Left = 288<br>    Top = 350<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button5'<br>    TabOrder = 5<br>    OnClick = Button5Click<br>end<br>object Edit1: TEdit<br>    Left = 184<br>    Top = 328<br>    Width = 321<br>    Height = 21<br>    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861<br>    TabOrder = 6<br>    Text = 'Edit1'<br>end<br>object Button6: TButton<br>    Left = 32<br>    Top = 304<br>    Width = 75<br>    Height = 25<br>    Caption = 'Button6'<br>    TabOrder = 7<br>    OnClick = Button6Click<br>end<br>object Edit2: TEdit<br>    Left = 369<br>    Top = 354<br>    Width = 96<br>    Height = 21<br>    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861<br>    TabOrder = 8<br>    Text = 'Edit1'<br>end<br>object IdTCPServer1: TIdTCPServer<br>    Bindings = &lt;<br>      item<br>      IP = '192.168.1.50'<br>      Port = 5050<br>      end&gt;<br>    DefaultPort = 0<br>    OnConnect = IdTCPServer1Connect<br>    OnExecute = IdTCPServer1Execute<br>    Left = 288<br>    Top = 48<br>end<br>object IdTCPClient1: TIdTCPClient<br>    OnConnected = IdTCPClient1Connected<br>    ConnectTimeout = 1000<br>    Host = '192.168.1.50'<br>    IPVersion = Id_IPv4<br>    Port = 5050<br>    ReadTimeout = 2000<br>    Left = 104<br>    Top = 40<br>end<br>object Timer1: TTimer<br>    Enabled = False<br>    Interval = 100<br>    OnTimer = Timer1Timer<br>    Left = 248<br>    Top = 352<br>end<br>object IdAntiFreeze1: TIdAntiFreeze<br>    Left = 88<br>    Top = 104<br>end<br>end</p>
<p>&nbsp;</p>
<p>------------------Form结束</p>
<p>-----------------------------------------------------------------------杂七杂八的小笔记------开始----------</p>
<p>-1----杂七杂八----开始</p>
<div class="cnblogs_code"><img id="code_img_closed_b6762386-e452-49ad-9283-76012ed3c177" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_b6762386-e452-49ad-9283-76012ed3c177" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_b6762386-e452-49ad-9283-76012ed3c177" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">unit</span><span style="color: rgba(0, 0, 0, 1)"> Unit1;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">interface</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">uses</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, ExtCtrls,
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">StdCtrls;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">type</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>   TFrmMainFiasSimulate = <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">(TForm)
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">    IdTCP_Issue: TIdTCPClient;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">    IdTCP_PMS: TIdTCPClient;
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">    Panel_Issue: TPanel;
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">    Panel_PMS: TPanel;
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">    GroupBox1: TGroupBox;
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">    Memo1: TMemo;
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_IssueMessage: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">    Button_SendMessage: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_IssueIP: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_IssuePort: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">    GroupBox2: TGroupBox;
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 0, 1)">    Memo2: TMemo;
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">    Edit2: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 0, 1)">    Button1: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_PMSIP: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_PMSPort: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">    Button_Connect: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">    Button_DisConnect: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">    Timer_Issue: TTimer;
</span><span style="color: rgba(0, 128, 128, 1)">31</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Button_ConnectClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">32</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Button_DisConnectClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">33</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Button_SendMessageClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">34</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Timer_IssueTimer(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">35</span>   <span style="color: rgba(0, 0, 255, 1)">private</span>
<span style="color: rgba(0, 128, 128, 1)">36</span>   <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)"> Private declarations </span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">37</span>   <span style="color: rgba(0, 0, 255, 1)">public</span>
<span style="color: rgba(0, 128, 128, 1)">38</span>   <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)"> Public declarations </span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">39</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">40</span>
<span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 0, 1)">FrmMainFiasSimulate: TFrmMainFiasSimulate;
</span><span style="color: rgba(0, 128, 128, 1)">43</span>
<span style="color: rgba(0, 128, 128, 1)">44</span> <span style="color: rgba(0, 0, 255, 1)">implementation</span>
<span style="color: rgba(0, 128, 128, 1)">45</span>
<span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">$R *.dfm</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">47</span>
<span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainFiasSimulate.Button_ConnectClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">50</span>   IdTCP_Issue.Host:=<span style="color: rgba(0, 0, 0, 1)">Edit_IssueIP.Text;
</span><span style="color: rgba(0, 128, 128, 1)">51</span>   IdTCP_Issue.Port:=<span style="color: rgba(0, 0, 0, 1)">StrToInt(Edit_IssuePort.Text);
</span><span style="color: rgba(0, 128, 128, 1)">52</span> <span style="color: rgba(0, 0, 0, 1)">IdTCP_Issue.Connect;
</span><span style="color: rgba(0, 128, 128, 1)">53</span>   IdTCP_Issue.IOHandler.DefStringEncoding:=TEncoding.Unicode;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TEncoding.ASCII</span>
<span style="color: rgba(0, 128, 128, 1)">54</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">55</span>
<span style="color: rgba(0, 128, 128, 1)">56</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainFiasSimulate.Button_DisConnectClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">58</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.Socket.Close;//IdTCP_Issue.Socket.InputBuffer.Clear;</span>
<span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 0, 0, 1)">IdTCP_Issue.DisConnect;
</span><span style="color: rgba(0, 128, 128, 1)">60</span>   IdTCP_Issue.Socket.InputBuffer.Clear;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.Socket.Close;</span>
<span style="color: rgba(0, 128, 128, 1)">61</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.Socket.CheckForDataOnSource()</span>
<span style="color: rgba(0, 128, 128, 1)">62</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">63</span>
<span style="color: rgba(0, 128, 128, 1)">64</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainFiasSimulate.Button_SendMessageClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">65</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">66</span>   IdTCP_Issue.IOHandler.DefStringEncoding:=TEncoding.Unicode;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TEncoding.ASCII;//TEncoding.Unicode;</span>
<span style="color: rgba(0, 128, 128, 1)">67</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.IOHandler.Write(#1);</span>
<span style="color: rgba(0, 128, 128, 1)">68</span> <span style="color: rgba(0, 0, 0, 1)">IdTCP_Issue.IOHandler.Write(Edit_IssueMessage.Text );
</span><span style="color: rgba(0, 128, 128, 1)">69</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">70</span>
<span style="color: rgba(0, 128, 128, 1)">71</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainFiasSimulate.Timer_IssueTimer(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">72</span> <span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 128, 1)">73</span>   vs:<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">74</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">75</span>   <span style="color: rgba(0, 0, 255, 1)">try</span>
<span style="color: rgba(0, 128, 128, 1)">76</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span> IdTCP_Issue.Connected<span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 128, 128, 1)">77</span> <span style="color: rgba(0, 0, 0, 1)">      Exit;
</span><span style="color: rgba(0, 128, 128, 1)">78</span>   vs:=IdTCP_Issue.IOHandler.ReadString(IdTCP_Issue.IOHandler.InputBuffer.Size ,TEncoding.Unicode);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有中文还是用 TEncoding.Unicode</span>
<span style="color: rgba(0, 128, 128, 1)">79</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">vs:=IdTCP_Issue.IOHandler.ReadString(IdTCP_Issue.IOHandler.InputBuffer.Size ,TEncoding.ASCII);</span>
<span style="color: rgba(0, 128, 128, 1)">80</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> vs&lt;&gt;<span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 128, 128, 1)">81</span> <span style="color: rgba(0, 0, 0, 1)">      Memo1.Lines.Add(vs) ;
</span><span style="color: rgba(0, 128, 128, 1)">82</span>   <span style="color: rgba(0, 0, 255, 1)">except</span>
<span style="color: rgba(0, 128, 128, 1)">83</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.Socket.Close;</span>
<span style="color: rgba(0, 128, 128, 1)">84</span>   <span style="color: rgba(0, 0, 255, 1)">try</span>
<span style="color: rgba(0, 128, 128, 1)">85</span> <span style="color: rgba(0, 0, 0, 1)">      IdTCP_Issue.DisConnect;
</span><span style="color: rgba(0, 128, 128, 1)">86</span>       IdTCP_Issue.Socket.InputBuffer.Clear ; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">IdTCP_Issue.Socket.Close这里这么写不行</span>
<span style="color: rgba(0, 128, 128, 1)">87</span> <span style="color: rgba(0, 0, 0, 1)">      IdTCP_Issue.Connect;
</span><span style="color: rgba(0, 128, 128, 1)">88</span>   <span style="color: rgba(0, 0, 255, 1)">except</span>
<span style="color: rgba(0, 128, 128, 1)">89</span>
<span style="color: rgba(0, 128, 128, 1)">90</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">91</span>
<span style="color: rgba(0, 128, 128, 1)">92</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">93</span>
<span style="color: rgba(0, 128, 128, 1)">94</span>
<span style="color: rgba(0, 128, 128, 1)">95</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">96</span>
<span style="color: rgba(0, 128, 128, 1)">97</span> <span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>-1----杂七杂八----结束</p>
<p>--2----杂七杂八----开始</p>
<div class="cnblogs_code"><img id="code_img_closed_2490beb8-ed6d-424e-a7fe-0ffeb8f8a2ff" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_2490beb8-ed6d-424e-a7fe-0ffeb8f8a2ff" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_2490beb8-ed6d-424e-a7fe-0ffeb8f8a2ff" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">unit</span><span style="color: rgba(0, 0, 0, 1)"> Unit1;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">interface</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">uses</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">Dialogs, IdContext, StdCtrls, IdBaseComponent, IdComponent, IdCustomTCPServer,
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">IdTCPServer;
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">type</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>   TFrmMainIssueCard = <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">(TForm)
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">    IdTCPServer1: TIdTCPServer;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">    Memo1: TMemo;
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">    Button1_StartService: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">    Button2_Stop: TButton;
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_IP: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">    Edit_Port: TEdit;
</span><span style="color: rgba(0, 128, 128, 1)">18</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> IdTCPServer1Execute(AContext: TIdContext);
</span><span style="color: rgba(0, 128, 128, 1)">19</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> IdTCPServer1Connect(AContext: TIdContext);
</span><span style="color: rgba(0, 128, 128, 1)">20</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> FormShow(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">21</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Button1_StartServiceClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">22</span>   <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> Button2_StopClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">23</span>   <span style="color: rgba(0, 0, 255, 1)">private</span>
<span style="color: rgba(0, 128, 128, 1)">24</span>   <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)"> Private declarations </span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">25</span>   <span style="color: rgba(0, 0, 255, 1)">public</span>
<span style="color: rgba(0, 128, 128, 1)">26</span>   <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)"> Public declarations </span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">27</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">FrmMainIssueCard: TFrmMainIssueCard;
</span><span style="color: rgba(0, 128, 128, 1)">31</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 255, 1)">implementation</span>
<span style="color: rgba(0, 128, 128, 1)">33</span>
<span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 255, 1)">uses</span>
<span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(0, 0, 0, 1)">IdGlobal,IdSocketHandle;
</span><span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 128, 0, 1)">{</span><span style="color: rgba(0, 128, 0, 1)">$R *.dfm</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 128, 128, 1)">38</span>
<span style="color: rgba(0, 128, 128, 1)">39</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainIssueCard.Button1_StartServiceClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 0, 0, 1)">vIdSocket:TIdSocketHandle;
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)">IdTCPServer1.Bindings.Clear;
</span><span style="color: rgba(0, 128, 128, 1)">44</span>   vIdSocket:=<span style="color: rgba(0, 0, 0, 1)">IdTCPServer1.Bindings.Add;
</span><span style="color: rgba(0, 128, 128, 1)">45</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">vIdSocket.IP:=Edit_IP.Text;</span>
<span style="color: rgba(0, 128, 128, 1)">46</span>   vIdSocket.Port:=<span style="color: rgba(0, 0, 0, 1)">StrToInt(Edit_Port.Text);
</span><span style="color: rgba(0, 128, 128, 1)">47</span>   IdTCPServer1.Active:=<span style="color: rgba(0, 0, 0, 1)">True;
</span><span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">49</span>
<span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainIssueCard.Button2_StopClick(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">52</span>   IdTCPServer1.Active:=<span style="color: rgba(0, 0, 0, 1)">False;
</span><span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">54</span>
<span style="color: rgba(0, 128, 128, 1)">55</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainIssueCard.FormShow(Sender: TObject);
</span><span style="color: rgba(0, 128, 128, 1)">56</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 0, 0, 1)">Memo1.Lines.Clear;
</span><span style="color: rgba(0, 128, 128, 1)">58</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">59</span>
<span style="color: rgba(0, 128, 128, 1)">60</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainIssueCard.IdTCPServer1Connect(AContext: TIdContext);
</span><span style="color: rgba(0, 128, 128, 1)">61</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">62</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> Length(AContext.Binding.PeerIP)&gt;<span style="color: rgba(128, 0, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">AContext.Connection.Socket.Binding.PeerIP;</span>
<span style="color: rgba(0, 128, 128, 1)">63</span>   <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">64</span>   AContext.Connection.IOHandler.DefStringEncoding:=<span style="color: rgba(0, 0, 0, 1)">TIdTextEncoding.Unicode;
</span><span style="color: rgba(0, 128, 128, 1)">65</span>   Memo1.Lines.Add(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">用户连接IP:</span><span style="color: rgba(128, 0, 0, 1)">'</span>+<span style="color: rgba(0, 0, 0, 1)">AContext.Binding.PeerIP);
</span><span style="color: rgba(0, 128, 128, 1)">66</span>   AContext.Binding.Send(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">连接成功!</span><span style="color: rgba(128, 0, 0, 1)">'</span>,TIdTextEncoding.Unicode); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TIdTextEncoding.ASCII</span>
<span style="color: rgba(0, 128, 128, 1)">67</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">68</span>
<span style="color: rgba(0, 128, 128, 1)">69</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">70</span>
<span style="color: rgba(0, 128, 128, 1)">71</span> <span style="color: rgba(0, 0, 255, 1)">procedure</span><span style="color: rgba(0, 0, 0, 1)"> TFrmMainIssueCard.IdTCPServer1Execute(AContext: TIdContext);
</span><span style="color: rgba(0, 128, 128, 1)">72</span> <span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 128, 1)">73</span> <span style="color: rgba(0, 0, 0, 1)">sCmd:Char;
</span><span style="color: rgba(0, 128, 128, 1)">74</span>   vs:<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">75</span> <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">76</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">sCmd := AContext.Connection.IOHandler.ReadChar; //先读取Char结构数据</span>
<span style="color: rgba(0, 128, 128, 1)">77</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if sCmd =#1 then</span>
<span style="color: rgba(0, 128, 128, 1)">78</span>
<span style="color: rgba(0, 128, 128, 1)">79</span>   <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">80</span>   vs:=AContext.Connection.IOHandler.Readstring(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.Unicode ); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TIdTextEncoding.Unicode</span>
<span style="color: rgba(0, 128, 128, 1)">81</span>   <span style="color: rgba(0, 0, 255, 1)">if</span> vs&lt;&gt;<span style="color: rgba(128, 0, 0, 1)">''</span> <span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 128, 128, 1)">82</span>   <span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 128, 1)">83</span>       Memo1.Lines.Add(AContext.Connection.Socket.Binding.PeerIP+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-----此次会话开始--------</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">84</span>       Memo1.Lines.Add(vs); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">AContext.Connection.IOHandler.InputBuffer.Size,</span>
<span style="color: rgba(0, 128, 128, 1)">85</span>       AContext.Connection.IOHandler.Write(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">您的消息服务器接收成功!</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,TIdTextEncoding.Unicode );
</span><span style="color: rgba(0, 128, 128, 1)">86</span>       Memo1.Lines.Add(AContext.Connection.Socket.Binding.PeerIP+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">-----此次会话结束--------</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">87</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">88</span>   <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">89</span>
<span style="color: rgba(0, 128, 128, 1)">90</span> <span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">91</span>
<span style="color: rgba(0, 128, 128, 1)">92</span> <span style="color: rgba(0, 0, 255, 1)">end</span>.</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>&nbsp;</p>
<p>--2----杂七杂八----结束</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>-----------------------------------------------------------------------杂七杂八的小笔记------结束----------</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/dmqhjp/p/14765447.html
頁: [1]
查看完整版本: Delphi中Indy10的IdTcpClient和IdTcpServer数据通信