PHP 8.5.0 Alpha 1 available for testing

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

John
15 years ago
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);
?>

<< Back to user notes page

To Top