gpt4 book ai didi

delphi-xe2 - Indy 10 TidTCPServer编码字符

转载 作者:行者123 更新时间:2023-12-02 17:35:12 29 4
gpt4 key购买 nike

Indy 10 socket 。 TIdTCPServer 组件。有一个客户端程序使用套接字连接到服务器程序。一切正常,通信没有问题,但我无法从服务器向客户端发送带有特殊字符的字符串。发送这样的字符串时:

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

客户端收到字符串:

usu?rio n?o existe

任何曾经使用过这个组件的人都知道如何正确设置编码并将这个特殊字符串发送到客户端?

最佳答案

默认情况下,Indy 使用 7 位 ASCII 作为其字符编码(为了与各种 Internet 协议(protocol)兼容)。要使用不同的字符编码,您需要:

  1. 在进行任何读/写之前设置IOHandler.DefStringEncoding 属性。基于字符串的 I/O 方法将默认使用此编码。

    // note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
    AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;
  2. 使用各种基于字符串的 I/O 方法的可选 AByteEncoding 参数,包括 WriteLn()

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

不用说,客户端在读取服务器数据时也必须使用等效编码,以便它可以将传输的字节解码回 Unicode。例如:

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

或者:

// note: use TIdTextEncoding.UTF8 if not using Indy 10.6 or later...
S := Client.IOHandler.ReadLn(IndyTextEncoding_UTF8);

关于delphi-xe2 - Indy 10 TidTCPServer编码字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27609332/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com