Re: Breaking out of a loop...
| From: | Tom Rogers | Date: | Wed, 12 May 2004 23:44:07 +0000 |
| Subject: | Re: Breaking out of a loop... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186057@lists.php.net to get a copy of this message | ||
Hi,
Thursday, May 13, 2004, 6:57:14 AM, you wrote:
RF> Hi,
RF> I have this code (below) that waits for particular data to come over
RF> the socket ("ENX"), at which point it breaks out of the loop and does
RF> other things. Presently, it will loop forever, until it receives
RF> "ENX"a good startbut I also need it to break-out if the loop runs
RF> longer than five seconds. This is where I'm having a problem. It seems
RF> that it is only reading once data comes over the socket, so it is stuck
RF> on that first 'while' line ("while(($buf =
RF> socket_read($socket,128,PHP_BINARY_READ)) !== false) {").
RF> $timer = time();
RF> while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) {
RF> $data .= $buf;
RF> $elapsed = time() - $timer;
RF> if(preg_match("/ENX/", $data)) {
RF> break;
RF> } elseif ($elapsed > 5) {
RF> echo "TOO LONG!\n";
RF> break;
RF> }
RF> }
RF> Maybe set non-blocking / blocking socket is the answer? Only problem is
RF> I can't find much [good] documentation describing what blocking and
RF> non-blocking sockets are good for, etc. Any ideas? Thanks.
RF> ...Rene
RF> --
RF> PHP General Mailing List (http://www.php.net/)
RF> To unsubscribe, visit: http://www.php.net/unsub.php
Try doing this at the error point
} elseif ($elapsed > 5) {
echo "TOO LONG!\n";
break 2; //kill both loops
}
--
regards,
Tom