5

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);
Buffer.BlockCopy(msgBytes, 0, msgToSend, prefixBytes.Length, msgBytes.Length);

As I understand, in PHP the function socket_send only accept strings. So, how can I do the same prefixing in PHP 5.x?

Update: I posted a follow-up question on how to process such prefixed data when received from a network socket.

Community
  • 1
  • 1
Mike
  • 1,992
  • 4
  • 31
  • 42

2 Answers2

6

In PHP strings are binary.

So you need to encode the integer length value as the binary representation of an unsigned integer as a 4-char (4 Octets; 32 bits) string. See pack:

# choose the right format according to your byte-order needs:

l   signed long (always 32 bit, machine byte order)
L   unsigned long (always 32 bit, machine byte order)
N   unsigned long (always 32 bit, big endian byte order)
V   unsigned long (always 32 bit, little endian byte order)

$string = pack('l', $length);
hakre
  • 193,403
  • 52
  • 435
  • 836
6

I guess you could use pack() to convert the number of bytes to a binary string. As you send your data over the network, you probably need to convert using the format "N" (unsigned long, always 32 bit, big endian byte order).

Here's an example:

$s="Hello World";
$length=pack("N",strlen($s));
socket_send($sock,$length.$s,4+strlen($s));
mistalee
  • 881
  • 6
  • 14
  • 2
    Why are you using "N" (big endian byte order) ? – Mike Mar 16 '12 at 20:33
  • 2
    @Mike: Please consult your .Net product documentation which type is applicable here. Probably a 32-bit signed integer, the endianness depends on your machine. Also check the specification of the protocol you're using. So `L`, `N` and `V` is likely to be wrong (even I had them in my answer, you need to double-check that with the specifications you're using). – hakre Mar 16 '12 at 20:52
  • 2
    @hakre thanks for your help. It seems that the c# app is using little-endian, so 'V' should be the best option, right? – Mike Mar 16 '12 at 21:33
  • 3
    @Mike: If I read the docs about `BitConverter.GetBytes(msgBytes.Length);` right, then it's a *signed* 32bit integer. Which could be probably `l` then. Check .net [`BitConverter.GetBytes`](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx) for the details, it shows which bytes it creates. Try to mimic that with `pack`. Or write a converter your own ;) – hakre Mar 16 '12 at 21:53
  • 2
    In my app it is a signed 32bit integer, little-endian. You say 'probably l' because it depends on the system PHP is running on (='machine byte order')? – Mike Mar 16 '12 at 22:09
  • See the BitConverter.GetBytes docs it has an example how the byte-order is with that convert. I can't tell you exactly which byte order that is, too late, sorry ;) I picked `l` because it's *signed*. – hakre Mar 17 '12 at 00:09