2

Indy 10 Sockets . TIdTCPServer component . There is a client program that connects to a server program using sockets. Everything working , there is no trouble in communicating , but I can not send a string with special characters from the server to the client. When sending a string like this:

         AContext.Connection.IOHandler.WriteLn ( ' Usuário não existe' ) ;

Client receives the string :

usu?rio n?o existe

Anyone who has ever worked with this component knows how do I set the encoding correctly and to send this special string to the client ?

Claudio Ferreira
  • 171
  • 2
  • 12

1 Answers1

7

By default, Indy uses 7bit ASCII as its character encoding (for compatibility with various Internet protocols). To use a different character encoding, you need to either:

  1. set the IOHandler.DefStringEncoding property before doing any reading/writing. The string-based I/O methods will then use this encoding by default.

    // note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
    AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;
    
  2. use the optional AByteEncoding parameter of the various string-based I/O methods, including WriteLn().

    // note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
    AContext.Connection.IOHandler.WriteLn('Usuário não existe', IndyTextEncoding_UTF8);
    

Needless to say, the client will also have to use an equivalent encoding when reading the server's data so it can decode the transmitted bytes back to Unicode. For example:

// note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
Client.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;

Or:

// note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
S := Client.IOHandler.ReadLn(IndyTextEncoding_UTF8);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Results after : AContext.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.ASCII; Usuario nao existe Results after : AContext.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.ANSI; Usu?rio n?o existe not solved . which encoding should I use? – Claudio Ferreira Dec 22 '14 at 20:05
  • Thank U. My mistakes were 2, the client was not using the equivalent Encoding to read, and second I should use UTF8 and not ASCII or ANSI. Now worked perfectly. Thanks again !!!! – Claudio Ferreira Dec 22 '14 at 20:26