Reputation: 3931
I have a simple server app written in Delphi using TTCPServer component it has a really basic OnAccept event procedure like below...
"Listener on 127.0.0.1 over port : 10000"
procedure TMainWindow.TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
S: String;
begin
S := ClientSocket.Receiveln();
ShowMessage(S);
ShowMessage(IntToStr(Length(S)));
Memo1.Lines.Add(S);
end;
And a simple php page like this...
<?php
$address = '127.0.0.1';
$port = 10000;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $address, $port);
socket_set_option($sock, SOL_SOCKET, SO_KEEPALIVE, 1);
$msg = 'Hello...!';
echo socket_write($sock, $msg, strlen($msg));
socket_close($sock);
?>
Now the problem is when I'm trying to write into the connected socket with php page no error occurred but the received text in the Delphi application (listener) show me the wrong result some thing like this "效汬⸮!"
what should I do ???
Upvotes: 6
Views: 2344
Reputation: 3931
As "Remy Lebeau" says Delphi 2009+ reads and writes string in Unicode form by default but the PHP decade about encoding according to string variable context. To solve the problem we need to use something like "Unicode2Ascii" function in Delphi listener application...
Upvotes: 1
Reputation: 5869
This function should serve your needs (I hope)
function UTF8ToUTF16(AUTF8String: RawByteString): String;
begin
SetCodePage(AUTF8String, 0, False);
Result := AUTF8String;
end;
Now you should be able to do this:
procedure TMainWindow.TcpServerAccept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
S: String;
begin
S := UTF8ToUTF16(ClientSocket.Receiveln());
ShowMessage(S);
ShowMessage(IntToStr(Length(S)));
Memo1.Lines.Add(S);
end;
Upvotes: 6