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 ([EMAIL PROTECTED]):
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




-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to