Please take a look at phpsocket-php Tutorial

Source: Internet
Author: User
Please take a look at phpsocket problems.
$appenStr = $buf = '';$time = time();for($i=0;$i<100;$i++){$buf = socket_read($msgsock, 8192);if(time() - $time > 5){break;}if($buf){$appenStr .= $buf;}else{break;}}echo $appenStr;

After reading a lot of information on the Internet, the socket_read function is blocked, causing the client program to remain suspended and waiting for the result of the above code to be returned.
But I can get the data.
$buf = socket_read($msgsock, 8192);$buf .= socket_read($msgsock, 8192);$buf .= socket_read($msgsock, 8192);
However, you cannot calculate the number of bytes. I have also tried the following code, but it is still suspended and loading.
$appenStr = $out = '';$start_time = time();do{if (false === ($out = socket_read($msgsock, 8192))){echo ("SOCKET_READ_ERROR: " . socket_strerror(socket_last_error($msgsock)));$str = "";break;}if (time() - $start_time > 1){echo ("SOCKET_READ_ERROR: Timeout!!!");$str = "";break;}$appenStr .= $out;} while ($out != "");

Thank you for your guidance.


Reply to discussion (solution)

If the input stream is empty and you read it again, a deadlock will occur (because nothing can be read again)
Therefore, it is very important to accurately determine whether to read null.
If (trim ($ buf )){
$ AppenStr. = $ buf;
} Else {
Break;
}

If the input stream is empty and you read it again, a deadlock will occur (because nothing can be read again)
Therefore, it is very important to accurately determine whether to read null.
If (trim ($ buf )){
$ AppenStr. = $ buf;
} Else {
Break;
}

Thank you. But I tried it.

Come on, please help me

I found the answer.

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.