note 23098 added to function.socket-select
| From: | julian dot haupt at gmx dot de | Date: | Tue, 09 Jul 2002 14:15:27 +0000 |
| Subject: | note 23098 added to function.socket-select | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32796@lists.php.net to get a copy of this message | ||
hello,
i just made a class which acts similiar to Perl's IO::Select in order to make socket selecting
very easy
your script should look something like that:
$server = new Server;
$client = new Client;
for (;;) {
foreach ($select->can_read(0) as $socket) {
if ($socket == $client->socket) {
// New Client Socket
$select->add(socket_accept($client->socket));
}
else {
//there's something to read on $socket
}
}
}
you should of course implement some routines to detect broken sockets and remove them from the
select object.
you can also do output buffering and check in the main-loop for sockets that are ready to write
class select {
var $sockets;
function select($sockets) {
$this->sockets = array();
foreach ($sockets as $socket) {
$this->add($socket);
}
}
function add($add_socket) {
array_push($this->sockets,$add_socket);
}
function remove($remove_socket) {
$sockets = array();
foreach ($this->sockets as $socket) {
if($remove_socket != $socket)
$sockets[] = $socket;
}
$this->sockets = $sockets;
}
function can_read($timeout) {
$read = $this->sockets;
socket_select($read,$write = NULL,$except = NULL,$timeout);
return $read;
}
function can_write($timeout) {
$write = $this->sockets;
socket_select($read = NULL,$write,$except = NULL,$timeout);
return $write;
}
}
--
http://www.php.net/manual/en/function.socket-select.php
http://master.php.net/manage/user-notes.php?action=edit+23098
http://master.php.net/manage/user-notes.php?action=delete+23098
http://master.php.net/manage/user-notes.php?action=reject+23098