简体   繁体   English

带有超时的Delphi Indy ReadLn

[英]Delphi Indy ReadLn with timeout

An Indy question. 一个印地问题。

I added a timeout parameter to my TIdTCPClient ReadLn call so my thread could check for terminated every so often. 我在TIdTCPClient ReadLn调用中添加了一个超时参数,因此我的线程可以经常检查终止。 However, if the timeout happens, I never get any data from the ReadLn from that point on. 但是,如果发生超时, ReadLn起,我永远不会从ReadLn获取任何数据。 How do I reset the TIdTCPClient so it will look for a line again? 如何重置TIdTCPClient ,使其再次寻找行?

procedure TClientListner.Execute;
var
  msg : String;

begin

  while not terminated do
  begin
    msg := fSocketCon.IOHandler.ReadLn('\n', 200);
    if not fSocketCon.IOHandler.ReadLnTimedOut then
    begin
      DoSomeThing(msg);
    end;
  end;
end;

Unlike in C/C++, \\ is not an escape character, so '\\n' is not interpreted as a line feed in Delphi. 与C / C ++不同, \\不是转义字符,因此'\\n'在Delphi中不解释为换行符。 It is a actual 2-character string, a '\\' character followed by a 'n' character. 它是一个实际的2个字符的字符串,一个'\\'字符后跟一个'n'字符。

To use a real line feed as the terminator, use #10 or Indy's LF constant instead: 要将实换行符用作终止符,请改用#10或Indy的LF常数:

msg := fSocketCon.IOHandler.ReadLn(#10, 200);

msg := fSocketCon.IOHandler.ReadLn(LF, 200);

Or, use a blank string, which tells ReadLn() to use its default LF terminator: 或者,使用一个空白字符串,该字符串告诉ReadLn()使用其默认的LF终止符:

msg := fSocketCon.IOHandler.ReadLn('', 200);

Or, don't use the ATimeout parameter at all. 或者,根本不要使用ATimeout参数。 Use the ReadTimeout property instead, and then don't specify a terminator so the default LF terminator is used: 请改用ReadTimeout属性,然后不指定终止符,因此使用默认的LF终止符:

fSocketCon.IOHandler.ReadTimeout := 200;
...
msg := fSocketCon.IOHandler.ReadLn;

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

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