0

I try to receive text in a idtcpclient but it does not work. 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. 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.

What I am doing wrong?

4
  • Does receivedtext = '' = false even compile? (Show your real code please)
    – mjn
    Apr 3, 2014 at 14:42
  • Why do you clear the InputBuffer? After a sucessful Read... operation, Indy will remove the read bytes from the InputBuffer.
    – mjn
    Apr 3, 2014 at 14:43
  • Why do you have this code in a timer? With the code as shown, you'll get re-entrancy issues, particularly with a timer value of 8ms.
    – Andy_D
    Apr 3, 2014 at 15:22
  • I think your code is completely wrong. Use google to search for examples, like ... sourceforge.net/projects/indy10clieservr First demo in there should help "1_sample Simple String Exchange"
    – smooty86
    Apr 3, 2014 at 16:27

1 Answer 1

8

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)

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:

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.