1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| //指令集的结尾是0xEF
$tcp_server = $serv->addlistener('0.0.0.0', 9503, SWOOLE_SOCK_TCP);
$tcp_server->set(array(
'open_length_check' => true,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_max_length' => 8192,
//'package_body_offset' => 4,
'open_eof_check' => true,
'package_eof' => 0xEF
));
//连接已经成功,打印了Client:Connect
$tcp_server->on('connect', function ($serv, $fd){
echo "Client:Connect\r\n";
});
//这是接收代码
$tcp_server->on('receive', function ($serv, $fd, $from_id, $data){
//$buffer = str_split(bin2hex($data), 2);
echo "开始接收数据^^^^";
$buffer = bin2hex($data);
var_dump($buffer);
echo hexdec($buffer[0]);
echo "!!!";
echo hexdec($buffer[1]);
echo "...";
//取所有数组之和
echo (hexdec($buffer[0])+hexdec($buffer[1]));
//将十进制数转为二进制
echo ( decbin (hexdec($buffer[0])+hexdec($buffer[1])));
$a=decbin (hexdec($buffer[0])+hexdec($buffer[1]));
echo ",,,,";
//取最后1个字节
echo substr($a,-8);
//二进制转十进制
echo "......";
echo bindec(substr($a,-8));
print($data);
$data = json_decode($data, true);
//已下省略业务代码
} |