zoukankan      html  css  js  c++  java
  • Delphi XE IdTCPClient 和 IdTCPServer 数据的发送与接收(indy10)

    -----------XE版,不是D7,XE的这个控件和D7有点不一样

    来源:

    Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)_136.la  https://www.136.la/shida/show-131002.html

    ---个人学习--仅仅是学习控件

    --这个学习的例子,有点问题,就是,想随时的自定义发送信息给客户端有问题,发送的消息不会显示(可能是没有收到,具体原因不知道,本人猜测,没有发送,只是缓存了,等下一次客户端发主动发消息过来,才能收到自定义的消息,暂时不去找具体原因,有时间再看看),但是客户端IdTCPClient发了新的消息,又收到自定义的信息

    ----IdTCPServer 、IdTCPClient - 程序园  http://www.voidcn.com/article/p-eplvabmi-bku.html

    ---------------------

    ------------ 

    这里也有一些有点用处的东西:https://www.cnblogs.com/dmqhjp/p/14765447.html 

    在这个链接的最下方可能会有点有用的东西,---杂七杂八---

    --------------

    -------------------------Unit

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, IdContext, StdCtrls, IdTCPConnection, IdTCPClient, IdBaseComponent,
    IdComponent, IdCustomTCPServer, IdTCPServer,IdGlobal, IdIOHandler,
    IdIOHandlerStream, IdIntercept, IdIOHandlerSocket, IdIOHandlerStack, ExtCtrls,
    IdAntiFreezeBase, IdAntiFreeze ;

    type
    TMData = record
    id:Integer;
    Name:Array[0..20] of Char;
    Age:Byte;
    UpdateTime:double;
    end;
    TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    IdTCPClient1: TIdTCPClient;
    Button4: TButton;
    Button5: TButton;
    Timer1: TTimer;
    Edit1: TEdit;
    Button6: TButton;
    IdAntiFreeze1: TIdAntiFreeze;
    procedure Button1Click(Sender: TObject);
    procedure IdTCPServer1Execute(AContext: TIdContext);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure IdTCPServer1Connect(AContext: TIdContext);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure IdTCPClient1Connected(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    private
    ClientIPList:TList;
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var
    SendD: TMData;
    begin
    if not IdTCPClient1.Connected then
    IdTCPClient1.Connect;
    SendD.ID := 10;
    StrPCopy(SendD.Name,'Delphi 您好');
    SendD.age := 18;
    SendD.UpdateTime := Now;
    IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
    IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据
    IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));

    end;

    procedure TForm1.Button4Click(Sender: TObject);
    var
    MyIdTCPConnection:TIdTCPConnection;
    vIOHandler:TIdIOHandler;
    begin
    try
    //MyIdTCPConnection:=TIdTCPConnection.Create(nil);
    //vIOHandler:=TIdIOHandler.Create(nil);
    //MyIdTCPConnection.IOHandler :=vIOHandler;
    //
    //ShowMessage(TIdContext(ClientIPList.Items[0]).Connection.Name+','+TIdContext(ClientIPList.Items[0]).binding.peerIp);

    //ShowMessage(IntToStr(TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Port)+',,'+TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Host);
    //MyIdTCPConnection.IOHandler.Port:=5050;
    //MyIdTCPConnection.IOHandler.Host:='192.168.1.50';
    //MyIdTCPConnection.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
    //MyIdTCPConnection.Socket.Binding.SetPeer('192.168.1.50',5050);
    //MyIdTCPConnection.Socket.Open ;
    //MyIdTCPConnection.IOHandler.Open;
    //MyIdTCPConnection.IOHandler.Write('Qaaam');
    //ShowMessage(TIdTCPConnection(ClientIPList.Items[0]));
    //TIdContext(ClientIPList.Items[0]).Connection.IOHandler.Write('AAAA');
    //TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Write(#10,TIdTextEncoding.ASCII);
    //TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Writeln('QAAAAN',TIdTextEncoding.ASCII);
    TIdContext(ClientIPList.Items[0]).Connection.IOHandler.CheckForDisconnect(True, True);
    TIdContext(ClientIPList.Items[0]).Connection.IOHandler.Write('QAAAAN',TIdTextEncoding.ASCII);
    //TIdTCPConnection(ClientIPList.Items[0]).SendCmd('888') ;
    finally
    //FreeAndNil(MyIdTCPConnection);
    //FreeAndNil(vIOHandler);

    end;
    end;

    procedure TForm1.Button5Click(Sender: TObject);
    begin
    Timer1.Enabled:=not Timer1.Enabled ;
    if Timer1.Enabled then
    Button5.Caption:='不显示'
    else
    Button5.Caption:='显示';
    //ShowMessage(IdTCPClient1.Intercept.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8 ));
    //ShowMessage(IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,) );
    end;

    procedure TForm1.Button6Click(Sender: TObject);
    begin
    //IdTCPClient1.IOHandler.Write(#10);
    if not IdTCPClient1.Connected then
    IdTCPClient1.Connect;
    IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
    //IdTCPClient1.IOHandler.Write(#0); //提前发送一个标识符,用于区分数据
    IdTCPClient1.IOHandler.Write('BAAAAC',TIdTextEncoding.ASCII);
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    FreeAndNil(ClientIPList);
    end;

    procedure TForm1.FormShow(Sender: TObject);
    begin
    ClientIPList:=TList.Create;
    IdTCPServer1.Active:=True;

    end;

    procedure TForm1.IdTCPClient1Connected(Sender: TObject);
    begin
    //ShowMessage('连接客户端');
    end;

    procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
    begin

    if Length(AContext.Binding.PeerIP)>5 then//AContext.Connection.Socket.Binding.PeerIP;
    ClientIPList.Add(AContext);

    end;

    procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
    var
    RData:TMData;
    buf:TIdBytes;
    sCmd:Char;
    sList:TStrings;
    I:Integer;
    ListCount:Integer;
    ss:string;
    begin
    //ACONTEXT.Connection.IOHandler.WriteLn('Recive Message!');
    //AContext.Connection
    {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('IP:'+AContext.Connection.Socket.Binding.PeerIP);
    Add('ID:'+Inttostr(RData.Id));
    Add('Name:'+StrPas(RData.Name));
    Add('Age:'+Inttostr(RData.age));
    Add('UpdateTime:'+DateTimeToStr(RData.UpdateTime));
    end;
    end //TCPClient.IOHandler.DefStringEncoding:=TEncoding.ASCII;
    else }
    //sCmd := AContext.Connection.IOHandler.ReadChar ;
    ss:=AContext.Connection.IOHandler.Readstring(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.ASCII );
    if ss<>'' then
    Memo1.Lines.Add(ss); //AContext.Connection.IOHandler.InputBuffer.Size,

    end;

    procedure TForm1.Timer1Timer(Sender: TObject);
    var
    ss:string;
    begin
    ss:=IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size);
    if ss<>'' then
    Edit1.Text:=ss;

    end;

    end.

    -------------------------Unit结束-------------
    Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)_136.la https://www.136.la/shida/show-131002.html
    ----------------------------资料开始---------------------------------------------------------

    1、IdTCPClient1 端 发送数据

    1.1 发送结构体:

    //定义结构体
    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类型

    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 发送一行字符串数据

    procedure TForm2.Button4Click(Sender: TObject);
    begin
    IdTCPClient1.IOHandler.Write(#10);
    IdTCPClient1.IOHandler.Write(‘Delphi测试‘,TIdTextEncoding.UTF8);
    end;
      

    2、IdTCPServer端 接收数据:

    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;

    ---------------------------资料结束---

    ---------------------------Form

    object Form1: TForm1
    Left = 0
    Top = 0
    Caption = 'Form1'
    ClientHeight = 383
    ClientWidth = 870
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    OldCreateOrder = False
    OnDestroy = FormDestroy
    OnShow = FormShow
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 32
    Top = 272
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Button2: TButton
    Left = 184
    Top = 296
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 1
    end
    object Button3: TButton
    Left = 312
    Top = 296
    Width = 75
    Height = 25
    Caption = 'Button3'
    TabOrder = 2
    end
    object Memo1: TMemo
    Left = 536
    Top = 8
    Width = 313
    Height = 313
    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    Lines.Strings = (
    'Memo1')
    TabOrder = 3
    end
    object Button4: TButton
    Left = 536
    Top = 350
    Width = 75
    Height = 25
    Caption = 'Button4'
    TabOrder = 4
    OnClick = Button4Click
    end
    object Button5: TButton
    Left = 288
    Top = 350
    Width = 75
    Height = 25
    Caption = 'Button5'
    TabOrder = 5
    OnClick = Button5Click
    end
    object Edit1: TEdit
    Left = 184
    Top = 328
    Width = 321
    Height = 21
    ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
    TabOrder = 6
    Text = 'Edit1'
    end
    object Button6: TButton
    Left = 32
    Top = 304
    Width = 75
    Height = 25
    Caption = 'Button6'
    TabOrder = 7
    OnClick = Button6Click
    end
    object IdTCPServer1: TIdTCPServer
    Bindings = <
    item
    IP = '192.168.1.50'
    Port = 5050
    end>
    DefaultPort = 0
    OnConnect = IdTCPServer1Connect
    OnExecute = IdTCPServer1Execute
    Left = 288
    Top = 48
    end
    object IdTCPClient1: TIdTCPClient
    OnConnected = IdTCPClient1Connected
    ConnectTimeout = 1000
    Host = '192.168.1.50'
    IPVersion = Id_IPv4
    Port = 5050
    ReadTimeout = 2000
    Left = 104
    Top = 40
    end
    object Timer1: TTimer
    Enabled = False
    Interval = 100
    OnTimer = Timer1Timer
    Left = 248
    Top = 352
    end
    object IdAntiFreeze1: TIdAntiFreeze
    Left = 88
    Top = 104
    end
    end

    ---------------------------Form

  • 相关阅读:
    数据库ACID
    tcp ip detatils
    process vs thread
    C++ virtual descructor
    static_cast dynamic_cast const_cast reinterpret_cast总结对比
    Meta Programming
    C++ traits
    c++内存管理
    洛谷 P4136 谁能赢呢?
    洛谷 P1166 打保龄球
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14595345.html
Copyright © 2011-2022 走看看