Delphi XE IdTCPClient 和 IdTCPServer 数据的发送与接收(indy10)
<p>-----------XE版,不是D7,XE的这个控件和D7有点不一样</p><p>来源:</p>
<p>Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)_136.la https://www.136.la/shida/show-131002.html</p>
<p> </p>
<p>---个人学习--仅仅是学习控件</p>
<p>--这个学习的例子,有点问题,就是,想随时的自定义发送信息给客户端有问题,发送的消息不会显示(可能是没有收到,具体原因不知道,本人猜测,没有发送,只是缓存了,等下一次客户端发主动发消息过来,才能收到自定义的消息,暂时不去找具体原因,有时间再看看),但是客户端IdTCPClient发了新的消息,又收到自定义的信息</p>
<p>----IdTCPServer 、IdTCPClient - 程序园 http://www.voidcn.com/article/p-eplvabmi-bku.html</p>
<p>---------------------</p>
<p>------------ </p>
<p>这里也有一些有点用处的东西:https://www.cnblogs.com/dmqhjp/p/14765447.html </p>
<p>在这个链接的最下方可能会有点有用的东西,---杂七杂八---</p>
<p>--------------</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> 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</p>
<p>{$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>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> TIdContext(ClientIPList.Items).Connection.IOHandler.CheckForDisconnect(True, True);<br> TIdContext(ClientIPList.Items).Connection.IOHandler.Write('QAAAAN',TIdTextEncoding.ASCII);<br> //TIdTCPConnection(ClientIPList.Items).SendCmd('888') ;<br>finally<br> //FreeAndNil(MyIdTCPConnection);<br> //FreeAndNil(vIOHandler);</p>
<p>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)>5 then//AContext.Connection.Socket.Binding.PeerIP;<br> ClientIPList.Add(AContext);</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<>'' then<br> Memo1.Lines.Add(ss); //AContext.Connection.IOHandler.InputBuffer.Size,</p>
<p>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>if ss<>'' then<br> Edit1.Text:=ss;</p>
<p>end;</p>
<p>end.</p>
<p>-------------------------Unit结束-------------<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>---------------------------资料结束---</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 IdTCPServer1: TIdTCPServer<br> Bindings = <<br> item<br> IP = '192.168.1.50'<br> Port = 5050<br> end><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>---------------------------Form</p><br><br>
来源:https://www.cnblogs.com/dmqhjp/p/14595345.html
頁:
[1]