I want to send JSON messages from a PHP script to a C # application over a network connection using PHP Sockets .
Typically for binary protocols, the first 4 bytes of each message should be an integer that represents the length (how many bytes) of the message.
In C #, I prefix each message with an integer that reports the length of the message as follows:
byte[] msgBytes = UTF8Encoding.UTF8.GetBytes("A JSON msg"); byte[] prefixBytes = BitConverter.GetBytes(msgBytes.Length); byte[] msgToSend = new byte[prefixBytes.Length + msgBytes.Length]; Buffer.BlockCopy(prefixBytes, 0, msgToSend, 0, prefixBytes.Length); Buffer.BlockCopy(msgBytes, 0, msgToSend, prefixBytes.Length, msgBytes.Length);
As I understand it, in PHP, the socket_send function only accepts strings. So how can I do the same prefix in PHP 5.x?
Update: I sent the following question about how to process such prefix data when it is received from a network socket.
source share