PHP uses Socket to send and receive hexadecimal data. code _ PHP Tutorial

Source: Internet
Author: User
PHP uses Socket to send and receive hexadecimal data. Recently, I am working on Socket communication in php. I found that many people on the network are learning how to send and receive data in hexadecimal mode. I have studied the code below. You are welcome to make a decision. Copy the code. for example, I recently did something about Socket communication in php. I found that many people on the network are learning how to send and receive data in hexadecimal mode. I have studied the code below. You are welcome to make a brick.

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

Bytes. Code such...

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.