Master to, about the socket in PHP problem!

Source: Internet
Author: User
Tags truncated
A hardware to send data to the server port (packet soft to get the correct 16), the data captured by the capture software is normal (16), however, through the socket of PHP data (through the conversion is also 16 binary, not converted is garbled), the following code, Please help the master to see where the wrong. Why PHP socket gets the data and grab the package software to get the same

The correct code obtained by the catch package software is (16 binary): FA, FF AA, 0B, xx A3
Data obtained via the PHP socket (via Dechex (), Ord () converted, and software fetch is not the same, is wrong): FA 7 2 FA 2 0 9 F6 4 2c, FA 1

Paste the following PHP socket code:
 = $MAX _users) {$reject = "Server full.                                   Try again later.\n ";                }//Put the current client connection as Socket_select select $connections [$i] = $newconn;                               Enter the connection resource cache container $writefds [$i] = $newconn;                Not connected properly if ($reject) {$close [$i] = true;                                  } else {echo "Welcome to the PHP Chat server!\n";                }//initializes the cache container for the current connection read content $input [$i] = "";            Continue            }//Client connection $i = (int) $rfd;            Read $tmp = @socket_read ($RFD, Php_normal_read);                if (! $tmp) {//Cannot read content 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 $remotePort." \ r \ n ";//$data =str_split ($buffer);//print_r ($data); $str =" \xfa\x01\x01\xff\xaa\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 AA 0 1 0 0 0 0 0 1

I don't see what the difference is.

Fully simulate your site and code

PHP 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
F......



You are the simulation of the data sent to the hardware is normal, my demand is: hardware data I accept the socket is not normal, through the packet capture tool to get the data is correct (FA, FF AA, 0B), and the data with the socket, in DOS The next is garbled, I passed Dechex (ORD (obtained data)) after conversion is: FA 7 2 FA 2 0 9 F6 4 2c in FA 1 (is wrong), I want to get the same data as the Grab kit tool (ie: FA) FF AA 03 00 01 00 0 B xx A3)

You have $line = Trim ($input [$i]); Is that the data you received?
1, remove trim, otherwise the incoming data may be truncated
2, because is binary data, you echo Base64_encode ($input [$i]); Post the results

You have $line = Trim ($input [$i]); Is that the data you received?
1, remove trim, otherwise the incoming data may be truncated
2, because is binary data, you echo Base64_encode ($input [$i]); Post the results




+gclavocaan2bcyi+geb+v/6aqh/qqoaaqaaaaaaafobaf+qqgabaaaa
This is the data that gets

$s = base64_decode (' +gclavocaan2bcyi+geb+v/6aqh/qqoaaqaaaaaaafobaf+qqgabaaaa ');p rint_r (Unpack (' H* ', $s));//or for ($ i=0; $i
  
  

You have $line = Trim ($input [$i]); Is that the data you received?
1, remove trim, otherwise the incoming data may be truncated
2, because is binary data, you echo Base64_encode ($input [$i]); Post the results


Is the data captured by the Grab tool (the correct data), how the 16-based data that is circled in the Shadow section is converted to the data referred to by the arrows, and how to turn back to the 16 binary

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

You're getting binary data, what are you going to do?

Those that you have in the red line are the hexadecimal representations of the data.
One byte is 8 bits, 1 hexadecimal digits are 4 bits
2 hexadecimal digits represent exactly one byte of the content.

The data I am now receiving with the PHP socket is the data that the arrows refer to (and placed in the variable $ A), and I now want to convert it to the data (that is, 16 binary) of the shadow part of the circle in the diagram.

You're getting binary data, what are you going to do?

Those that you have in the red line are the hexadecimal representations of the data.
One byte is 8 bits, 1 hexadecimal digits are 4 bits
2 hexadecimal digits represent exactly one byte of the content.



The data I am now receiving with the PHP socket is the data that the arrows refer to (and placed in the variable $ A), and I now want to convert it to the data (that is, 16 binary) of the shadow part of the circle in the diagram.

Didn't I #6 give you the code?

It feels a little messy, sorry.

Do you have an escape character?

Didn't I #6 give you the code?



Now it is possible to get the code, but 3 16 less data

The code obtained is: FA072502FA020009F6042C88FA0101FAFFFA0101FFAA030001000B00

The clutch is obtained: FA, FF AA, 0B, xx A3

That is, the less the XX A3

 
    

Can perfectly solve your problem ... I just saw this question of yours.
  • 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.