Socket communication php serves as the client, and C ++ serves as the server communication problem.

Source: Internet
Author: User
Socket communication php serves as the client, and C ++ serves as the server communication problem. Recently, I want to make something like this. now php can send successfully, and C ++ makes the correct response: Send a json string, however, php cannot receive messages sent by C ++... I don't know why .. Could you please help me? is there a problem with the message format sent from C ++ to php? I think the key issue lies in socket_read .. But I don't understand
 ";}While ($ out = socket_read ($ socket, 8192) {// echo" received the server's return message! \ N "; // $ obj = json_decode ($ out); // echo" the accepted content is: ", $ out; echo $ out; // echo $ obj-> 0;}/* if (false! ==( $ Bytes = socket_recv ($ socket, $ buf, 2048, MSG_WAITALL) {echo 'A '. $ buf. 'A'; echo "Read $ 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, please give me some suggestions

Json encode of php and C ++ is different. here we need to handle it.

Php makes a log to debug all the collected data. first, confirm whether the information is not received.

Don't sink, please give me some suggestions


I haven't done json parsing yet. now I just want to get the entire json string, but I can't parse it...

Json encode of php and C ++ is different. here we need to handle it.


$ Out = socket_read ($ socket, 8192) if I change this sentence 8192 to 1, I can read 9 characters but not all...

Since you think that the server has not returned the code, it seems that you should paste your C ++ server code.

Json encode of php and C ++ is different. here we need to handle it.


Socket_read error found, but no error prompt .... Is there a function similar to the socket_read function? I have also tried socket_recv .. No

While ($ out = socket_read ($ socket, 8192 ))! = False ){
}
Remove the space characters

While (true ){
$ Out = socket_read ($ socket, 8192 );

Socket_read returns any received data, including empty strings.
Therefore, you should not continue to read it, instead of reading it once.

Of course, since the Loop turns into an endless loop, you need to make an agreement to jump out of the loop.

While ($ out = socket_read ($ socket, 8192 ))! = False ){
}
Remove the space characters




While (true ){
$ Out = socket_read ($ socket, 8192 );

Socket_read returns any received data, including empty strings.
Therefore, you should not continue to read it, instead of reading it once.

Of course, since the Loop turns into an endless loop, you need to make an agreement to jump out of the loop.


Well, I made an agreement to jump out of the loop, that is, to change socket_read ($ socket, 8192) to socket_read ($ socket, 1) to read, and the read is fragmented, why ....

You don't understand what I mean!
Socket_read cannot be used as a condition for loop termination.
For example, while (socket_read ($ socket, 1) will terminate the loop once you read the characters that php considers as "null" (which are the true characters returned by empty), resulting in incomplete data.
While (socket_read ($ socket, 8192), if you have not received the Terminator \ 0, \ r, \ n, it will always stop waiting for input.
Of course, it is also possible to read the remaining null characters in the previous round at the beginning, but it cannot enter the loop body.

Since echo "closes SOCKET... \ n" after the loop ends ";
Echo "$ out to close SOCKET... \ n ";
See what can be output

You don't understand what I mean!
Socket_read cannot be used as a condition for loop termination.
For example, while (socket_read ($ socket, 1) will terminate the loop once you read the characters that php considers as "null" (which are the true characters returned by empty), resulting in incomplete data.
While (socket_read ($ socket, 8192), if you have not received the Terminator \ 0, \ r, \ n, it will always stop waiting for input.
Of course, it is also possible to read the remaining null characters in the previous round at the beginning, but it cannot enter the loop body.

Since echo "closes SOCKET... \ n" after the loop ends ";
Echo "$ out to close SOCKET... \ n ";
See what can be output


I understand what you mean. I changed the code to this, but as I said, only $ out = socket_read ($ socket, 1, if $ out = socket_read ($ socket, 8192) is an endless loop, waiting, and nothing can be output .. Even the previous output information cannot be 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;

The code is stuck wrong just now. it should be like this. I added a string to the server and finally the letter E.

That is, if the Terminator \ 0, \ r, \ n is not received, it will always stop waiting for input.
This indicates that the server does not send an ending sign (that is, it does not comply with the conventions)
You can only connect the result of socket_read ($ socket, 1 ).
In fact, reading one byte is the same as reading one byte (both read from the input buffer zone)

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.