How to write byte by byte to socket in PHP?

PatlaDJ picture PatlaDJ · Mar 18, 2010 · Viewed 10.4k times · Source

How to write byte by byte to socket in PHP?

For example how can I do something like:

socket_write($socket,$msg.14.56.255.11.7.89.152,strlen($msg)+7);

The pseudo code concatenated digits are actually bytes in dec. Hope you understand me.

Answer

Mark Tomlin picture Mark Tomlin · Mar 18, 2010

You can use the pack function to pack the data into any datatype you wish. Then send it with any of the socket functions.

$strLen = strlen($msg);

$packet = pack("a{$strLen}C7", $msg, 14, 56, 255, 11, 7, 89, 152);

$pckLen = strlen($packet);

socket_write($socket, $packet, $pckLen);