The following is an analysis of the implementation code for sending and receiving hexadecimal data through Socket in PHP. For more information about Socket communication in php, I found that many people on the network are learning how to send and receive data in hexadecimal mode. The Code is as follows, welcome to shoot bricks.
The Code is as follows:
$ SendStr = '30 32 30 34 03 30 33'; // hexadecimal data
$ SendStrArray = str_split (str_replace ('','', $ sendStr), 2); // convert hexadecimal data into two arrays.
$ Socket = socket_create (AF_INET, SOCK_STREAM, getprotobyname ("tcp"); // create a Socket
If (socket_connect ($ socket, "192.168.1.100", 8080) {// connection
For ($ j = 0; $ j <count ($ sendStrArray); $ j ++ ){
Socket_write ($ socket, chr (hexdec ($ sendStrArray [$ j]); // send group-by-group data
}
$ ReceiveStr = "";
$ ReceiveStr = socket_read ($ socket, 1024, PHP_BINARY_READ); // receives data in binary mode.
$ ReceiveStrHex = bin2hex ($ receiveStr); // convert binary data into hexadecimal data
Echo "client:". $ receiveStrHex;
}
Socket_close ($ socket); // close the Socket
?>