Home > Article > Backend Development > PHP implements the method of sending hexadecimal socket
This article mainly shares with you the method of sending hexadecimal socket in PHP. It is mainly shared with you in the form of code. I hope it can help you.
<?php //~ $sendStr = '7d 2c 00 00 00 00'; // 16进制数据 $sendStr = "\x7D\x2c\x00\x00\x00\x00"; // 16进制数据 //~ $sendStrArray = str_split(str_replace(' ', '', $sendStr), 2); // 将16进制数据转换成两个一组的数组 $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp")); // 创建Socket if (socket_connect($socket, "127.0.0.1", 50001)) { //连接 socket_write($socket, $sendStr); // 逐组数据发送 //~ for ($j = 0; $j < count($sendStrArray); $j++) { //~ socket_write($socket, chr(hexdec($sendStrArray[$j]))); // 逐组数据发送 //~ } $receiveStr = ""; $receiveStr = socket_read($socket, 1024, PHP_BINARY_READ); // 采用2进制方式接收数据 $receiveStrHex = bin2hex($receiveStr); // 将2进制数据转换成16进制 echo "client:" . $receiveStrHex; } socket_close($socket); // 关闭Socket ?>
Related recommendations:
How to implement socket in php
Detailed example of socket programming in php
Detailed explanation of socket communication in php
The above is the detailed content of PHP implements the method of sending hexadecimal socket. For more information, please follow other related articles on the PHP Chinese website!