How to handle received data in the TCPClient ? (Delphi - Indy)

后端 未结 5 613
梦如初夏
梦如初夏 2021-02-01 22:59

When i send a message from TCPClient to a TCPServer it will be handled using OnExecute event in the server . Now i want to handle the rece

5条回答
  •  鱼传尺愫
    2021-02-01 23:35

    Here is my code to Read / Write with Delphi 7. Using the Tcp Event Read.

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ScktComp;
    
    type
      TForm1 = class(TForm)
        ClientSocket1: TClientSocket;
        Button1: TButton;
        ListBox1: TListBox;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
        procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
          ErrorEvent: TErrorEvent; var ErrorCode: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     UsePort: Integer;
     UseHost: String;
    
    begin
    UseHost := Edit1.Text;
    UsePort := STRTOINT(Edit2.Text);
    ClientSocket1.Port :=  UsePort;
    ClientSocket1.Host :=  UseHost;
    ClientSocket1.Active :=  true;
    end;
    
    procedure TForm1.ClientSocket1Read(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
    ListBox1.Items.Add(ClientSocket1.Socket.ReceiveText);
    
    end;
    
    procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      ErrorCode:=0;
      ClientSocket1.Active := False;
    end;
    
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      ClientSocket1.Socket.SendText(Edit1.Text);
    end;
    
    end.
    

提交回复
热议问题