关于IdTcpClient的使用

关于IdTcpClient的使用


其实Indy的block模式挺好玩的.但是它没有类似于IdTcpServer的OnExcute事件.
为此,我们必须自己写一个从TThread继承的类,用于检查Statck中的Tcp有没有收到数据(另一种方法相似,只是在另一个线程中直接读取数据).
 
这里是第一种方法的代码:
Code: type
  TDataAvailableEvent = procedure(Sender: TObject; Connection: TIdTCPConnection) of object;


  TListener = class(TThread)
  private
    FConnection: TIdTCPConnection;
    FOnDataAvailable: TIdTCPConnection;
  protected
    procedure Execute; override;
    procedure DataAvailable; virtual;
  public
    property Connection: TIdTCPConnection read FConnection write FConnection;
    property OnDataAvailable: TDataAvailableEvent read FOnDataAvailable write FOnDataAvailable;
  end;




function ConnectionHasData(Connection: TIdTCPConnection): Boolean;
begin
  if Connection.Connected then
  begin
    Result := Connection.InputBuffer.Size > 0;
    if not Result then
    begin
      Connection.ReadFromStack(False, 1, False);
      Result := Connection.InputBuffer.Size > 0;
    end;
  end
  else
    Result := False;
end;






procedure TListener.Execute; 
begin
  while not Terminated do
  begin
    if ConnectionHasData(FConnection) then
      Synchronize(DataAvailable)
    else
      Sleep(1);
  end;
end;


procedure TListener.DataAvailable;
begin
  if Assigned(FOnDataAvailable) then
    FOnDataAvailable(Self, FConnection);
end; 




Code:   TForm1 = class(TForm)
  ...
  private
    FListener: TListener;
  end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  FListener := TListener.Create(True);
  FListener.Connection := IdTCPClient;
  FListener.OnDataAvailable := EvDataAvailable;
  FListener.Resume; // start
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin


 // Listener
  FListener.Terminate;
  while FListener.Suspended do FListener.Resume;
  FListener.WaitFor;
  FListener.Free;
end;




procedure TForm1.EvDataAvailable(Sender: TObject; Connection: TIdTCPConnection);
begin
  Memo1.Lines.Add(Connection.ReadLn);
end; 
 
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页