Simple socket with php and delphi?

I have a simple server application written in Delphi using the TTCPServer component, which has a really basic OnAccept event procedure, as shown below ...

"Listener on 127.0.0.1 through 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 that when I try to write to the connected socket with a php page, an error did not occur, but the received text in the Delphi application (listener) showed me the wrong result, something like this "效 汬 ⹯⸮!"

what should I do???

+6
source share
2 answers

As "Remy Lebeau" says, Delphi 2009+ reads and writes a Unicode string by default, but PHP's decade is about encoding according to a string variable context. To solve the problem, we need to use something like the "Unicode2Ascii" function in the application for listening to Delphi ...

+1
source

This feature should serve your needs (hopefully)

 function UTF8ToUTF16(AUTF8String: RawByteString): String; begin SetCodePage(AUTF8String, 0, False); Result := AUTF8String; end; 

Now you can do it:

 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; 
+6
source

Source: https://habr.com/ru/post/914491/


All Articles