Home > Back-end >  Through the c # SOCKET send Chinese, Delphi become after receiving???????
Through the c # SOCKET send Chinese, Delphi become after receiving???????

Time:11-04

C # server:

Void appServer_NewRequestReceived (AppSession session, StringRequestInfo requestInfo)
{
String MSG.
MSG="success!" ;
The session. The Send (MSG);
}

DELPHI client:
Procedure TForm1. ClientSocket1Read (Sender: TObject; Sockets: TCustomWinSocket);
Var s: Ansistring;
The begin
S:=Socket. ReceiveText;
Edit2. Text:=s;//here s return to the question mark???
end;


ReceiveText returns the AnsiString, I think it is the problem of coding, c # STRING what is the default, DELPHI side how to convert after receiving?

The function TCustomWinSocket. ReceiveText: AnsiString;
The begin
SetLength (Result, ReceiveBuf (Pointer (nil) ^, 1));
SetLength (Result, ReceiveBuf (Pointer (Result) ^, Length (Result)));
end;

CodePudding user response:

S:=PAnsiChar (AnsiString (Socket. ReceiveText));

CodePudding user response:

reference 1/f, joke zare response:
s:=PAnsiChar (AnsiString (Socket. ReceiveText));

No effect,

CodePudding user response:

Should be you use Delphi version too old, before the 2009 version of the basic does not support unicode (except WideString type), in high version,
If you don't want to use this version, you can give TCustomWinSocket. Add an overloaded methods:

The function TCustomWinSocket. ReceiveText: AnsiString; The phrase ";
The begin
SetLength (Result, ReceiveBuf (Pointer (nil) ^, 1));
SetLength (Result, ReceiveBuf (Pointer (Result) ^, Length (Result)));
end;

The function TCustomWinSocket. ReceiveText: WideString; The phrase ";
The begin
SetLength (Result, ReceiveBuf (Pointer (nil) ^, 1));
SetLength (Result, ReceiveBuf (Pointer (Result) ^, Length (Result) div SizeOf (WideChar));
end;

TCustomWinSocket class declaration section also want to join the overloading method statement, and the project build again (note TCustomWinSocket source files in the search path)

Then you can:
Procedure TForm1. ClientSocket1Read (Sender: TObject; Sockets: TCustomWinSocket);
Var s: WideString;
The begin
S:=Socket. ReceiveText;
Edit2. Text:=s;
end;

CodePudding user response:

reference 3 floor early play play nuclear response:
should be Delphi version too old, you use before the 2009 version of the basic does not support unicode (except WideString type), in high version,
If you don't want to use this version, you can give TCustomWinSocket. Add an overloaded methods:

The function TCustomWinSocket. ReceiveText: AnsiString; The phrase ";
The begin
SetLength (Result, ReceiveBuf (Pointer (nil) ^, 1));
SetLength (Result, ReceiveBuf (Pointer (Result) ^, Length (Result)));
end;

The function TCustomWinSocket. ReceiveText: WideString; The phrase ";
The begin
SetLength (Result, ReceiveBuf (Pointer (nil) ^, 1));
SetLength (Result, ReceiveBuf (Pointer (Result) ^, Length (Result) div SizeOf (WideChar));
end;

TCustomWinSocket class declaration section also want to join the overloading method statement, and the project build again (note TCustomWinSocket source files in the search path)

Then you can:
Procedure TForm1. ClientSocket1Read (Sender: TObject; Sockets: TCustomWinSocket);
Var s: WideString;
The begin
S:=Socket. ReceiveText;
Edit2. Text:=s;
end;


Used is 2010, I should also support, I redefine ReceiveTex return value to a String or WideString all not line, become all are garbled (AnsiString is become in Chinese? No.)

CodePudding user response:

Only Chinese garbled, could send the character encoding is utf-8

CodePudding user response:

Don't tube what server you use Tencoding directly. The default. Get string (buf) is finished

CodePudding user response:

If your Delphi is xe version, specified directly coded format,

AContext. Connection. The IOHandler. ReadString (IndyTextEncoding_8Bit);
IndyTextEncoding_8Bit can under the following format:

IndyTextEncoding_Default: IIdTextEncoding;
IndyTextEncoding_OSDefault: IIdTextEncoding;
IndyTextEncoding_8Bit: IIdTextEncoding;
IndyTextEncoding_ASCII: IIdTextEncoding;
IndyTextEncoding_UTF16BE: IIdTextEncoding;
IndyTextEncoding_UTF16LE: IIdTextEncoding;
IndyTextEncoding_UTF7: IIdTextEncoding;
IndyTextEncoding_UTF8: IIdTextEncoding;

CodePudding user response:

C # is the default Unicode, converted to the same character set is ok,
  • Related