Come on, questions about socket in php

Source: Internet
Author: User
Come on, about socket in php! A hardware sends data to the server port (the packet capture software gets the correct hexadecimal format), and the data captured by the packet capture software is normal (in hexadecimal format ), however, the data received through the socket in php (after conversion, it is also in hexadecimal format. if not converted, it is garbled). The following is the code, about socket in php!
A hardware sends data to the server port (the packet capture software gets the correct hexadecimal format), and the data captured by the packet capture software is normal (in hexadecimal format ), however, the data received through the socket in php (after conversion, it is also in hexadecimal format. if the conversion is not performed, it is garbled.) the following code is pasted. please help me to see what went wrong. Why does the data obtained by The php socket differ from those obtained by the packet capture software?

The correct code obtained through the packet capture software is (hexadecimal): FA 01 01 ff aa 03 00 01 00 0B 00 00 00 A3
The data obtained through the php socket (converted by dechex () and ord () is different from the data captured by the software, which is wrong ): fa 7 25 2 fa 2 0 9 f6 4 2c 88 fa 1

The following is the php socket code:
PHP code
  
  = $ MAX_USERS) {$ reject = "Server full. try again later. \ n ";}// set the current client connection to socket_select and select $ connections [$ I] = $ newconn; // enter the connection resource cache container $ writefds [$ I] = $ newconn; // The connection is abnormal if ($ reject) {$ close [$ I] = true ;} else {echo "Welcome to the PHP Chat Server! \ N ";}// initialize the cache container for the content read by the current connection $ input [$ I] =" "; continue ;}// client connection $ I = (int) $ rfd; // read $ tmp = @ socket_read ($ rfd, 14, PHP_NORMAL_READ); if (! $ Tmp) {// The content cannot be read. print "connection closed on socket $ I \ n"; close ($ I); continue;} $ input [$ I]. = $ tmp; $ tmp = substr ($ input [$ I],-1);/* if ($ tmp! = "\ R" & $ tmp! = "\ N") {// no end of line, more data coming continue;} */$ line = trim ($ input [$ I]); $ input [$ I] = ""; echo 'Client> '. $ line. "\ r \ n"; $ data = str_split ($ line); print_r ($ data); echo "\ r \ n "; // $ data = explode ("\ t", $ line); // $ data = str_split ($ data); foreach ($ data as $ v) {echo dechex (ord ($ v )). "\ t";} echo "\ r \ n"; socket_getpeername ($ connections [$ I], & $ remoteIP, & $ remotePort); echo $ remoteIP. "\ r \ n"; echo $ remo TePort. "\ r \ n"; // $ data = str_split ($ buffer); // print_r ($ data ); $ str = "\ xFA \ x01 \ x01 \ xFF \ xAA \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 "; socket_send ($ connections [$ I], $ str, strlen ($ str), 0);} foreach ($ writefds as $ wfd) {$ I = (int) $ wfd; $ w = socket_write ($ wfd, "hello") ;}} function close ($ I) {global $ connections, $ input, $ close; socket_shutdown ($ connections [$ I]); socket_close ($ connections [$ I]); unset ($ connecti Ons [$ I]); unset ($ input [$ I]); unset ($ close [$ I]);}?>

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.