1

In my TIdTCPServer.OnConnect event, I receive information using:

AContext.Connection.IOHandler.ReadLn(LF, 5000)

I've read that ReadLn() expects LF on the end of strings, so do I need to add a LF at the end, like this?

TCPClient.IOHandler.WriteLn('TEXT' + LF);

I see in WriteLn() it already include that automatically:

procedure TIdIOHandler.WriteLn(const AOut: string;
  AByteEncoding: IIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  );
begin
                                                                                  
  // which encodes a LF character to byte $25 instead of $0A (and decodes
  // byte $0A to character #$8E instead of #$A).  To account for that, don't
  // encoding the CRLF using the specified encoding anymore, force the encoding
  // to what it should be...
  //
  // But, what to do if the target encoding is UTF-16?
  {
  Write(AOut, AByteEncoding{$IFDEF STRING_IS_ANSI, ASrcEncoding{$ENDIF);
  Write(EOL, Indy8BitEncoding{$IFDEF STRING_IS_ANSI, Indy8BitEncoding{$ENDIF);
  }

  // Do as one write so it only makes one call to network
  Write(AOut + EOL, AByteEncoding
    {$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF}
    );
end;

1 Answer 1

2

As you noted, IOHandler.WriteLn() automatically sends EOL (aka CRLF) at the end of each string. This is because most textual Internet protocols use CRLF.

IOHandler.ReadLn() expects LF by default if you omit the terminator, but whenever the terminator is LF (implicitly or explicitly) then IOHandler.ReadLn() will also handle CRLF as well. So, DO NOT pass in your own LF to IOHandler.WriteLn(), eg:

TCPClient.IOHandler.WriteLn('TEXT');

If you want to send LF instead of CRLF, use IOHandler.Write() instead, eg:

TCPClient.IOHandler.Write('TEXT' + LF);
6
  • What's the difference from LF to CRLF? as said i use AContext.Connection.IOHandler.ReadLn(LF, 5000). so i don't need add "+ LF" at end of WriteLn on client side?
    – PSDEVS
    Oct 4 at 13:45
  • 1
    LF is 1 byte 0x0A. CRLF is 2 bytes 0x0D 0x0A. Most protocols standardized on CRLF, but many implementations are linient to accept just LF. As for using +LF with WriteLn(), I literally covered that in my answer. I would suggest using the ReadTimeout property instead of passing a timeout to ReadLn() directly, then you can omit the LF parameter, too. Then your code is just WriteLn('TEXT') and ReadLn(), which look more consistent with each other. Oct 4 at 14:45
  • you mean AContext.Connection.IOHandler.ReadTimeout?
    – PSDEVS
    Oct 4 at 15:18
  • 1
    @PSDEVS Yes. You can certainly specify a timeout on each individual read if you really want to, but that is usually overkill. You can set the ReadTimeout up front instead, and then it will apply to all subsequent read operations. So then ...ReadLn(LF, 5000) becomes ...ReadLn(LF), and LF is the default so that can be removed, too: ...ReadLn() Oct 4 at 16:11
  • Where add ReadTimeout instead at Context and on ReadLn?
    – PSDEVS
    Oct 4 at 16:43

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.