views:

58

answers:

1

host:127.00.0.1; port:5001; ReadTimeout:3000;

//Following codes to get the response
procedure TForm1.Button2Click(Sender: TObject);
var
  s:string;
begin
  try
    while True do
    begin
      s := s+IdTCPClient1.IOHandler.ReadChar();
    end;
  finally
     showmessage(s);
....other operations...
  end;
//....

When timerout the part of other operations will not be excuted.Any ideas to contionu the code?Thanks.

A: 

ReadChar() will raise an EIdReadTimeout exception if the ReadTimeout elapses. You need to use a try/except block to catch that, eg:

procedure TForm1.Button2Click(Sender: TObject);
var
  s: string;
begin
  try
    repeat
      try
        s := s + IdTCPClient1.IOHandler.ReadChar();
      except
        on E: EIdReadTimeout do Break;
      end;
    until False;
  finally
    ShowMessage(s);
    ...
  end;
//.... 

A better option is to not call ReadChar() in a loop at all. Use the IOHandler's CheckForDataOnSource() and InputBufferAsString() methods instead, eg:

procedure TForm1.Button2Click(Sender: TObject);
var
  s: string;
begin
  try
    while IdTCPClient1.IOHandler.CheckForDataOnSource(IdTimeoutDefault) do begin end;
  finally
    s := IdTCPClient1.IOHandler.InputBufferAsString;
    ShowMessage(s);
    ...
  end; 
//.... 
Remy Lebeau - TeamB
It's nice!Thanks
Jeason