note 98222 added to function.socket-select
| From: | John at osu1 dot php dot net | Date: | Wed, 02 Jun 2010 16:25:57 +0000 |
| Subject: | note 98222 added to function.socket-select | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169826@lists.php.net to get a copy of this message | ||
The way the document describes socket_select()'s handling of sockets polled for read is rather
obscure.
It says that it checks to see if reading would not "block," but the overall description of
socket_select() says it checks for a change in blocking status. Unfortunately, these are in
conflict.
If a socket already has data in the buffer, calling socket_select() on that socket would never
return (assuming null timeout), and would block forever. :-( This is because the blocking status
wouldn't change. It simply stays "non-blocking"
It is important to remember NOT to select() on a socket which may already have data available.
An example...
<?php
//... $socket is already here...
$done = false;
$n = 0;
do{
$tmp = 0;
$r = $w = $e = array();
$r = array($socket);
socket_select($r,$w,$e,null);
$n = socket_recv($socket, $tmp, 1024, 0);
//$done = true; //Something determines that we are done reading...
}while(!$done);
?>
This MAY NOT work... socket_select() is always being called... but we may have data in the input
buffer.
We need to ensure that the last time we read, nothing was read... (empty buffer)
<?php
//... $socket is already here...
$done = false;
$n = 0;
do{
$tmp = 0;
$r = $w = $e = array();
$r = array($socket);
if($n === 0) socket_select($r,$w,$e,null);
$n = socket_recv($socket, $tmp, 1024, 0);
//$done = true; //Something determines that we are done reading...
}while(!$done);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 95.211.2.161 (proxied: 127.0.0.1)
----
Manual Page -- http://www.php.net/manual/en/function.socket-select.php
Edit -- https://master.php.net/note/edit/98222
Del: integrated -- https://master.php.net/note/delete/98222/integrated
Del: useless -- https://master.php.net/note/delete/98222/useless
Del: bad code -- https://master.php.net/note/delete/98222/bad+code
Del: spam -- https://master.php.net/note/delete/98222/spam
Del: non-english -- https://master.php.net/note/delete/98222/non-english
Del: in docs -- https://master.php.net/note/delete/98222/in+docs
Del: other reasons-- https://master.php.net/note/delete/98222
Reject -- https://master.php.net/note/reject/98222
Search -- https://master.php.net/manage/user-notes.php