Search code examples
encodingtcpclientindyindy-9

Indy 9 TCP client text encoding


I have a Delphi 7 app using Indy 9 which connects a TIdTCPClient to a TIdTCPServer in a Delphi XE2 app using Indy 10.5.

To set the text encoding to UTF-8 in TIdTCPClient in Indy 10, I can use this:

TCPClient.IOHandler.DefStringEncoding := TIdTextEncoding.UTF8;

How can I set the text encoding to UTF-8 in Indy 9, since there is no DefStringEncoding property in the IOHandler?


Solution

  • Indy 9 does not support encodings, and also does not support Delphi 2009+ for Unicode. Everything in Indy 9 assumes/is based on AnsiString only. Strings are transmitted as-is as if they were raw byte arrays.

    So, don't send your AnsiString data over a connection using ANSI. You can send/receive it as UTF-8 instead. You just have to encode/decode the AnsiString data manually, that's all. Indy 9 will send a UTF-8 encoded AnsiString as-is, and read a UTF-8 encoded AnsiString as-is. You can then encode/decode the AnsiString data to/from UTF-8 as needed in your surrounding code.

    For example, your Indy 9 client can do this:

    IdTCPClient1.WriteLn(UTF8Encode('Some Unicode String Here'));
    ...
    S := UTF8Decode(IdTCPClient1.ReadLn);
    

    Then your Indy 10 server can do this:

    AContext.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.UTF8;
    ...
    S := AContext.Connection.IOHandler.ReadLn;
    ...
    AContext.Connection.IOHandler.WriteLn(...);