Come on, 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 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:
 = $ 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 $ remotePor T. "\ 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 ($ connections [$ I]); unset ($ input [$ I]); unset ($ close [$ I]);}?>


Reply to discussion (solution)

Fully simulate your site and code

$str="\xFA\x01\x01\xFF\xAA\xAA\x00\x01\x00\x00\x00\x00\x00\x01";$line = $str;$data = str_split($line);foreach($data as $v){  echo dechex(ord($v))."\t";}
Get
Fa 1 1 ff aa 0 1 0 0 0 0 1

No difference

Fully simulate your site and code

PHP code

$ Str = "\ xFA \ x01 \ x01 \ xFF \ xAA \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x01 ";

$ Line = $ str;
$ Data = str_split ($ line );
Foreach ($ data as $ v ){
Echo dechex (ord ($ v). "\ t ";
}

Get
F ......



What you simulate is that the data sent to the hardware is normal, and my requirement is: the data sent by the hardware is not accepted by socket normally, the data obtained through the packet capture tool is correct (FA 01 01 ff aa 03 00 01 00 0B 00 00 00 A3), and the data obtained using socket is garbled in dos, after I use dechex (ord (obtained data) to convert to fa 7 25 2 fa 2 0 9 f6 4 2c 88 fa 1 (which is incorrect ), what I want is the same data as that obtained by the packet capture tool (that is, FA 01 01 01 FF AA 03 00 01 00 0B 00 00 A3)

You have $ line = trim ($ input [$ I]); is this accepted data?
1. remove trim. Otherwise, the imported data may be truncated.
2. because it is binary data, you echo base64_encode ($ input [$ I]); paste the result

You have $ line = trim ($ input [$ I]); is this accepted data?
1. remove trim. Otherwise, the imported data may be truncated.
2. because it is binary data, you echo base64_encode ($ input [$ I]); paste the result




+ GclAvoCAAn2BCyI + gEB + v/6AQH/qqoAAQAAAAAAAfoBAf + qqgABAAAA
This is the obtained data.

$ S = base64_decode ('+ gclAvoCAAn2BCyI + gEB + v/6AQH/qqoAAQAAAAAAAfoBAf + qqgABAAAA'); print_r (unpack ('H * ', $ s )); // or for ($ I = 0; $ I
  
  

You have $ line = trim ($ input [$ I]); is this accepted data?
1. remove trim. Otherwise, the imported data may be truncated.
2. because it is binary data, you echo base64_encode ($ input [$ I]); paste the result


It refers to the data captured by the packet capture tool (correct data). in the figure, how do we convert the hexadecimal data of the shadow part into the data indicated by the arrow, and how to convert the data back to the hexadecimal format?

[Img = http://bbs.php100.com/attachment/Mon_1208/18_219521_355456b7c8c862b.jpg? 93] [/img]

What you receive is binary data. What are you going to do?

The items in your red line are the hexadecimal representation of the data.
One byte is eight binary bits, and one hexadecimal number is four binary bits
Two hexadecimal numbers represent the content of one byte.

The data I receive with php socket is the data pointed by the arrow (and put in variable $ ), I want to convert her into the shadow data (in hexadecimal format) in the circle. how can I convert it?

What you receive is binary data. What are you going to do?

The items in your red line are the hexadecimal representation of the data.
One byte is eight binary bits, and one hexadecimal number is four binary bits
Two hexadecimal numbers represent the content of one byte.



The data I receive with php socket is the data pointed by the arrow (and put in variable $ ), I want to convert her into the shadow data (in hexadecimal format) in the circle. how can I convert it?

I #6. didn't I give you the code?

I'm sorry.

Are there escape characters?

I #6. didn't I give you the code?



The code can be obtained now, but three hexadecimal data entries are missing.

The obtained code is: fa072502fa020009f6042c88fa0101fafffa0101ffaa030001000b00.

The packet capture result is: FA 01 01 01 ff aa 03 00 01 00 0B 00 00 A3

That is, 00 A3 is missing.

   

It can perfectly solve your problems ...... I just saw your problem.

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.