Re: Breaking out of a loop...
| From: | René Fournier | Date: | Wed, 12 May 2004 22:36:22 +0000 |
| Subject: | Re: Breaking out of a loop... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186054@lists.php.net to get a copy of this message | ||
Thanks for the help, but.... I now get the error "PHP Warning: socket_set_option() expects exactly 4 parameters, 3 given in /Users/test/cr.php on line 51" when I run the script. Also, it still seems to just wait on that "while" line, instead of skipping to the "} elseif ($elapsed > 5) {" and breaking from the loop (which is what I want it to do). (Is it just me, or is the PHP documentation on socket_set_block and socket_set_option a bit too sparse? I mean, where do you find the docs on those options and values?)
---------------------------------------------------------------------------------------------------------
socket_set_block($socket);
socket_set_option($socket,SO_RCVTIMEO,5000); // milliseconds iirc
do {
$timer = time();
while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) {
$data .= $buf;
$elapsed = time() - $timer;
if(preg_match("/ENX/", $data)) {
break;
} elseif ($elapsed > 5) {
echo "TOO LONG!\n";
break;
}
if ($error = socket_last_error($socket)) {
echo "socket error [$error]:" . socket_strerror($error);
}
}
---------------------------------------------------------------------------------------------------------
On Wednesday, May 12, 2004, at 03:41 PM, Curt Zirzow wrote:
* Thus wrote Ren Fournier (m5@renefournier.com):Hi, I have this code (below) that waits for particular data to come over the socket ("ENX"), at which point it breaks out of the loop and does other things. Presently, it will loop forever, until it receives "ENX"—a good start—but I also need it to break-out if the loop runs longer than five seconds. This is where I'm having a problem. It seems that it is only reading once data comes over the socket, so it is stuck on that first 'while' line ("while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) {"). $timer = time(); while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) { $data .= $buf; $elapsed = time() - $timer; if(preg_match("/ENX/", $data)) { break; } elseif ($elapsed > 5) { echo "TOO LONG!\n"; break; } } Maybe set non-blocking / blocking socket is the answer? Only problem is I can't find much [good] documentation describing what blocking and non-blocking sockets are good for, etc. Any ideas? Thanks.blocking - wait for response or till timeout non-blocking - return right away If you use blocking and then set the timeout for the read, you can just check for socket_last_error() after the loop. socket_set_block($socket); socket_set_option($socket, SO_RCVTIMEO, 5000); // milliseconds iirc while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) { //... } if ($error = socket_last_error($socket) ) { echo "socket error [$error]:" . socket_strerror($error); } or something like that. Curt --"I used to think I was indecisive, but now I'm not so sure." --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php