I am using php to connect to a socket server to send binary data - ie not standard ascii (or printable) strings. For example a message may contain ascii 0 or any number from 0 - 255.
I have functions for example like this:
function append_uint16($str, $num) {
$str += chr($num % 256);
$num /= 256;
$str += chr($num);
return $str;
}
It is called like this:
$msg = "\xA7\xA7";
$payload_size = 9 + 1 + strlen($param1) + 1 + strlen($param2);
$msg += append_uint16($msg, $payload_size);
Then I am sending to a socket server like this:
function send_msg($host, $port, $msg) {
$fp = stream_socket_client("tcp://" . $host . ":" . $port, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />
";
} else {
fwrite($fp, $msg);
}
}
But the messages are not correctly formed. I suspect that my string processing is not quite correct. Or maybe I can't use strings in php in this way? Any ideas?