简体   繁体   English

Indy TIdTCPClient 接收文本

[英]Indy TIdTCPClient receive text

I try to receive text in a idtcpclient but it does not work.我尝试在 idtcpclient 中接收文本,但它不起作用。 This is the code that I use in a timer:这是我在计时器中使用的代码:

procedure TForm1.Timer2Timer(Sender: TObject);
var
  receivedtext:string;
begin
  if idtcpclient1.Connected = true then
  begin
    with idtcpclient1 do
    begin
      if not IOHandler.InputBufferIsEmpty then
      begin
        try
          receivedtext := IOHandler.ReadLn;
        finally
          if receivedtext = '' = false then
          begin
            showmessage(receivedtext);
            idtcpclient1.IOHandler.InputBuffer.Clear;
            receivedtext := '';
          end;
        end;
      end;
    end;
  end
  else
  begin
    timer2.Enabled := false;
  end;
end;

The interval of the timer is 8 ms.定时器的间隔为 8 毫秒。 The timer is automatically enabled on connect.计时器在连接时自动启用。 But I don't get an messagebox or an error when I send something.但是当我发送一些东西时,我没有收到消息框或错误。 I am sure that I written the data because when I use tclientsocket I do receive it.我确信我写了数据,因为当我使用tclientsocket我确实收到了它。

What I am doing wrong?我做错了什么?

Use something more like this instead:使用更像这样的东西:

procedure TForm1.Timer2Timer(Sender: TObject);
var
  receivedtext: string;
begin
  with IdTCPClient1 do
  begin
    try
      if IOHandler.InputBufferIsEmpty then
      begin
        IOHandler.CheckForDataOnSource(0);
        IOHandler.CheckForDisconnect;
        if IOHandler.InputBufferIsEmpty then Exit;
      end;
      receivedtext := IOHandler.ReadLn;
    except
      Timer2.Enabled := False;
      Exit;
    end;
    if receivedtext <> '' then
      ShowMessage(receivedtext);
  end;
end;

With that said, this kind of code would be better implemented using a worker thread instead of a timer.话虽如此,这种代码最好使用工作线程而不是计时器来实现。

1 - Create a new class derived from TThread (File > New > Other > Thread Object) 1 - 创建一个从TThread派生的新类(文件 > 新建 > 其他 > 线程对象)

type
  TDataEvent = procedure(const Data: string) of object;

  TReadingThread = class(TThread)
  private
    FClient: TIdTCPClient;
    FData: string;
    FOnData: TDataEvent;
    procedure DataReceived;
  protected
    procedure Execute; override;
  public
    constructor Create(AClient: TIdTCPClient); reintroduce;
    property OnData: TDataEvent read FOnData write FOnData;
  end;

constructor TReadingThread.Create(AClient: TIdTCPClient);
begin
  inherited Create(True);
  FClient := AClient;
end;

procedure TReadingThread.Execute;
begin
  while not Terminated do
  begin
    FData := FClient.IOHandler.ReadLn;
    if (FData <> '') and Assigned(FOnData) then
      Synchronize(DataReceived);
  end;
end;

procedure TReadingThread.DataReceived;
begin
  if Assigned(FOnData) then
    FOnData(FData);
end;

2 - Modify your connection code: 2 - 修改您的连接代码:

IdTCPClient1.Connect;
try
  Thread := TReadingThread.Create(IdTCPClient1);
  Thread.OnData := DataReceived;
  Thread.Resume;
except
  IdTCPClient1.Disconnect;
  raise;
end;

...

if Assigned(Thread) then Thread.Terminate;
try
  IdTCPClient1.Disconnect;
finally
  if Assigned(Thread) then
  begin
    Thread.WaitFor;
    FreeAndNil(Thread);
  end;
end;

...

procedure TForm1.DataReceived(const Data: string);
begin
  ShowMessage(Data);
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM