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.