Sending sockets data with a leading length value
I want to send JSON messages from a PHP script to a C# app over a network connection using PHP Sockets . Usually, for binary protocols, the first 4 bytes of every message must be an integer which represents the length (how many bytes) of the message. In C# I prefix every message with an integer that tells the length of the message as follow: 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);