------------------------------------------------------------------------ ---------------------------------
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