Claudio Ferreira
Claudio Ferreira

Reputation: 171

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?

Upvotes: 0

Views: 2332

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595320

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(...);

Upvotes: 0

Claudio Ferreira
Claudio Ferreira

Reputation: 171

I have not found a way to set the Encoding UTF8 in Indy 9, then decided as follows: Knowing what the default encoding in Indy 9 TCPClient is ANSI , I configured the Encoding to ANSI in TCPServer app, converted any UTF8 strings to ANSI before comunications from TCPserver to TCPClient and its all resolved.

Upvotes: 0

Related Questions