TIdTCPClient也没有一个类似于TIdTCPserver的Execute这样的事件,那么如果服务端主动发数据给TIdTCPClient,则它怎么接收?我找了些资料,说用timer来收,可是我用定时器,怎么一运行就死!再说,用timer效率也不高呀.有没有其它解决办法?最好有代码!谢谢!

解决方案 »

  1.   

    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
      RzButton, StdCtrls, Mask, RzEdit;type
      TForm2 = class(TForm)
        IdTCPClient1: TIdTCPClient;
        RzEdit1: TRzEdit;
        RzButton1: TRzButton;
        RzButton2: TRzButton;
        procedure RzButton1Click(Sender: TObject);
        procedure RzButton2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      TtextTcpClient=class(TThread)
        private
          temp_string:string;
          procedure IOinput;
        public
          procedure execute;override;
        end;
    var
      Form2: TForm2;
        textClient:TtextTcpClient;
    implementationuses main;{$R *.dfm}
    procedure TtextTcpClient.IOinput;
    var
    temp:string;
    begin
      temp:=temp_string;
    end;
    procedure TtextTcpClient.execute;
    var
    temp:string;
    begin
      if (not textClient.Terminated) and (Form2.IdTCPClient1.Connected) then
        begin
          temp:=#13+#10;
          Form2.IdTCPClient1.ReadLn(temp);
          Synchronize(IOinput);
        end;
    end;procedure TForm2.RzButton1Click(Sender: TObject);
    var
    temp:string;
    begin
      temp:=RzEdit1.Text;
      Form2.IdTCPClient1.Write(temp);
    end;procedure TForm2.RzButton2Click(Sender: TObject);
    var
    temp:string;
    begin
      temp:=RzEdit1.Text;
      temp:=temp+main.CRC_pack(temp,length(temp));
      Form2.IdTCPClient1.Write(temp);
    end;procedure TForm2.FormCreate(Sender: TObject);
    begin
      try
      IdTCPClient1.Host:='127.0.0.1';
      IdTCPClient1.Port:=8006;
      if not IdTCPClient1.Connected then
        begin
          textClient:=TtextTcpClient.Create(true);
          textClient.FreeOnTerminate:=True;
          textClient.Resume;
          //IdTCPClient1.Connect;
        end;
      except  end;end;end.
      

  2.   

    谢谢 Blue8228()
    还有,请问,我参考了你的代码,客户端可以接收到服务端的数据,但是怎么线程只能接收一次呢?第二次就接收不到呢?
      

  3.   

    if (not textClient.Terminated) and (Form2.IdTCPClient1.Connected) then把他上面代码的这句里的if改成while, then 改成 do 就可以接收不只一次了。