PHP uses Socket to send and receive hexadecimal data.

Source: Internet
Author: User
The following is an analysis of the implementation code for sending and receiving hexadecimal data through Socket in PHP. For more information, see

The following is an analysis of the implementation code for sending and receiving hexadecimal data through Socket in PHP. For more information, see

I recently made some content about Socket communication in php. I found many people on the network are learning how to send and receive data in hexadecimal mode. I have studied the Hong Kong server. The Code is as follows: server space, hong Kong server. Click it.

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
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.