Socket looping challenge

From: Date: Thu, 13 May 2004 19:21:10 +0000
Subject: Socket looping challenge
Groups: php.general 
Request: Send a blank email to php-general+get-186149@lists.php.net to get a copy of this message
Hi all, Still encountering some challenges with my socket loop. Basically, I need this socket client to wait to read incoming data, but if nothing happens for more than three seconds, I want it to do something, then return to waiting (for another three seconds). I've got some good help from Curt and Tom (thanks guys), but there are evidently still issues with my code. Here's simplified snippet: --------------------------------------------------------------------------------------------------------------------- CODE --------------------------------------------------------------------------------------------------------------------- // INFINITE LOOP $msg_recv = false; $timeout = array('sec' => 3, 'usec' => 0); socket_set_block($socket); socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout); do { while(($buf = socket_read($socket,128,PHP_BINARY_READ)) !== false) { $data .= $buf; if(preg_match("/ENX/",$data)) { $msg_recv = true; break; } elseif ($error = socket_last_error($socket)) { $msg_recv = false; echo "\nTIMEOUT: SOCKET ERROR [$error]:" . socket_strerror($error); break; } } if ($msg_recv == true) { echo "\nMESSAGE RECEIVED\n"; $msg_recv = false; } else { echo "\nTIMEOUT: MESSAGE NOT RECEIVED\n"; $msg_recv = false; } $data = ""; } while ($connection == true); --------------------------------------------------------------------------------------------------------------------- OUTPUT --------------------------------------------------------------------------------------------------------------------- MESSAGE RECEIVED MESSAGE RECEIVED PHP Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 61 Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 61 TIMEOUT: MESSAGE NOT RECEIVED PHP Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 61 Warning: socket_read() unable to read from socket [35]: Resource temporarily unavailable in /Users/rene/Sites/gpspolice/titan/cr.php on line 61 TIMEOUT: MESSAGE NOT RECEIVED

« previous php.general (#186149) next »