Socket communication PHP As a client, C + + as a service-side communication problem.

Source: Internet
Author: User
Recently want to do a title such things, now PHP send can succeed, C + + to make the correct corresponding: send a JSON string, but the C + + message sent by PHP can not receive ... Don't know why this is. Would you please help me? Is there a problem with the message format that C + + passes to PHP? The key question I feel on the socket_read. But I can't figure it out.
 TCP/IP Connection\ n "; $port = 6000; $ip =" 127.0.0.1 "; $socket = Socket_create (Af_inet, Sock_stream, sol_tcp); if ($socket < 0) {echo ' s Ocket_create () Failed:reason: ". Socket_strerror ($socket). "\ n";} else {echo "ok.\n";} echo "Attempted to connect ' $ip ' port ' $port ' ... \ n"; $result = Socket_connect ($socket, $ip, $port); if ($result < 0) {echo "Socket_conn ECT () Failed.\nreason: ($result) ". Socket_strerror ($result). "\ n";} else {echo "Connect ok\n";} $in = ' r '; $out = '; if (!socket_write ($socket, $in, strlen ($in))) {echo "Socket_write () Failed:reason:". Socket_strerror ($socket). "\ n";} else {echo "sent to server information successfully! \ n "; echo" sends the content: $in
";} while ($out = Socket_read ($socket, 8192)) {//echo "Receive server backhaul information successfully! \ n "//$obj =json_decode ($out); echo "accepts the content as:", $out; echo $out;//echo $obj->0;} /*if (False!== ($bytes = Socket_recv ($socket, $buf, 2048, Msg_waitall)) {echo ' AA '. $buf. ' AA '; echo "Read $bytes bytes from Socket_recv (). Closing socket ... ";} else {echo "socket_recv () failed; Reason: ". Socket_strerror (Socket_last_error ($socket)). " ";} */echo "Close socket...\n"; Socket_close ($socket); echo "Close ok\n";? >


Reply to discussion (solution)

Don't sink, the gods give some advice.

PHP and C + + JSON encode is different, here to deal with.

PHP will collect all the data to make a log for easy debugging. First confirm that no information was received

Don't sink, the gods give some advice.


I haven't done JSON parsing yet, but I'm just trying to get the whole JSON string down, but it's not coming out ...

PHP and C + + JSON encode is different, here to deal with.


$out = Socket_read ($socket, 8192) If I change this sentence 8192 to 1, I can read 9 characters, but I can't read them all ...

Since you think the server side does not return, it seems that you should post your C + + server-side code.

PHP and C + + JSON encode is different, here to deal with.


Socket_read error was found, but no error message .... Is there a function similar to Socket_read function? Socket_recv I've tried, too. No way

while ($out = Socket_read ($socket, 8192))!== false) {
}
Here, let's get rid of the factors that read the space character

while (true) {
$out = Socket_read ($socket, 8192);

Socket_read will return any data received, including an empty string
So you don't have to read it all the time, not the end.

Of course, since the loop turns into a dead loop, you need to make a convention that jumps out of the loop.

while ($out = Socket_read ($socket, 8192))!== false) {
}
Here, let's get rid of the factors that read the space character




while (true) {
$out = Socket_read ($socket, 8192);

Socket_read will return any data received, including an empty string
So you don't have to read it all the time, not the end.

Of course, since the loop turns into a dead loop, you need to make a convention that jumps out of the loop.


Well, I made an appointment to jump out of the loop, is to change Socket_read ($socket, 8192) to Socket_read ($socket, 1) to read, and read the fragmented, please this is why ....

You don't know what I mean!
Socket_read is a condition that cannot be terminated as a loop.
For example while (Socket_read ($socket, 1)), the loop is terminated as soon as PHP is read as "empty" characters (empty returns true characters), resulting in incomplete data
While the while (Socket_read ($socket, 8192)), if the Terminator has not been received, \ r, \ n is never stopped waiting for input
Of course, it is possible to read the last remaining empty characters at the beginning, but not the loop body at all.

Since you have echo "Close socket...\n" at the end of the cycle;
echo "$out close socket...\n" may be changed;
See what you can output.

You don't know what I mean!
Socket_read is a condition that cannot be terminated as a loop.
For example while (Socket_read ($socket, 1)), the loop is terminated as soon as PHP is read as "empty" characters (empty returns true characters), resulting in incomplete data
While the while (Socket_read ($socket, 8192)), if the Terminator has not been received, \ r, \ n is never stopped waiting for input
Of course, it is possible to read the last remaining empty characters at the beginning, but not the loop body at all.

Since you have echo "Close socket...\n" at the end of the cycle;
echo "$out close socket...\n" may be changed;
See what you can output.


I know what you mean, I changed the code to this, but like I said, only $out=socket_read ($socket, 1), if $out=socket_read ($socket, 8192) is a dead loop, waiting, nothing is output. Even if the previous output information is not output.
do {$out =socket_read ($socket, 1); $data. = $out;} while (true); Echo $data;

do {$out =socket_read ($socket, 8192), if ($out = = "E") break, $data. = $out;} while (true); Echo $data;

Just now the code is wrong, it should be, I added a string at the end of the server with the letter E

That's not the case. If you have not received the Terminator, \ r, \ n will always be stopped waiting for input.
This means that the server does not have a send terminator (that is, it does not follow the Convention)
You can only connect the results of Socket_read ($socket, 1).
Actually read a piece and read a byte efficiency is the same (all read from the input buffer)

  • 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.