Вопрос

I've been facing a problem this morning and simply couldn't solve. I'm trying to send a reply message from a TIdCmdTCPServer to a TIdTCPClient, but I'm getting '?' characters where the message shows non-ANSCII characters. I found out that Indy uses ASCII encoding and tried to change it, to no result. Here's the code:

uses IdIOHandler, IdGlobal;

procedure TDMSocket.DataModuleCreate(Sender: TObject);
begin
  TIdIOHandler.DefStringEncoding:= TIdTextEncoding.UTF8;
end;

procedure TDMSocket.IdCmdTCPServerLoginCommand(ASender: TIdCommand);
begin
  [...]
  InformaErros('Usuário ou senha inválidos', ASender);
  [...]
end;

procedure TDMSocket.InformaErros(Erro: string; ASender: TIdCommand);
begin
  ASender.Reply.Text.Text:= Erro;
end;

This way, I get the following error:

Property 'DefStringEncoding' Inacessible Here

Trying to access DefStringEncoding on the IOHandler porperty of IdCmdTCPServer raises the same error. And I didn't get any success with GIdDefaultAnsiEncoding.

What can I do to send the message with the right characters?

Это было полезно?

Решение

DefStringEncoding is not a class property, which is why you cannot access it via the TIdIOHandler class type like you attempted to do. It is an instance property instead, so you need a pointer to an instance of a TIdIOHandler object.

In the OnCommand events, you can reach the TIdIOHandler object via the TIdCommand.Context.Connection.IOHandler property.

If all of your commands and replies will use UTF-8 then it would be better to set DefStringEncoding one time when a client connects to the server. You can do that in the OnConnect event via the TIdContext.Connection.IOHandler property.

As for GIdDefaultAnsiEncoding, TIdIOHandler only uses it when DefStringEncoding is nil, which it is not by default (there is a TODO item to make TIdIOHandler use GIdDefaultAnsiEncoding when initializing DefStringEncoding).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top