socket_select() can also serve as a more granular sleep():
<?php
# half-second sleep
$undef = array();
socket_select($undef, $undef, $undef, 0, "500000");
?>
socket_select
(PHP 4 >= 4.1.0, PHP 5)
socket_select — Executa a chamada de sistema select() nos arrays dados de sockets com um timeout especificado
Descrição
Esta função é EXPERIMENTAL. O comportamento desta função, seu nome, incluindo toda documentação pode ser modificado sem aviso em futuras versões do PHP. Esta função deve ser usada por sua própria conta e risco.
socket_select() aceita arrays de sockets e aguarda até que seu status seja alterado. Estes vem com "background" de sockets BSD irá reconhecer aquele array de socket resource são de fato a chamada ao arquivo descritor de configuração (fds). Três arrays independentes de socket resources são observados.
A lista de sockets no array read irá ser mostrada para ver se o caractere disponível anterior à leitura (mais precisamente, para ver se a leitura não está obstruída - em particular, um socket resource está também no fim-de-arquivo (EOF) , neste caso o socket_read() irá retornar uma string com comprimento zero.
A lista de sockets no array write irá ser visualizada para mostrar se não é uma escrita não está obstruída.
A lista de sockets no array except irá mostrar caso haja exceções.
Na saída, os arrays são modificados para indicar qual socket resource atualmente alterou seu status.
Você não precisa passar cada array para socket_select(). Você pode deixá-los de fora e usar um array vazio ou NULL ao invés disso. Também não se esqueça que esses arrays são passados by reference e irão ser modificados após o retorno de socket_select().
Exemplo #1 Exemplo de socket_select()
<?php
/* Prepare the read array */
$read = array($socket1, $socket2);
$num_changed_sockets = socket_select($read, $write = NULL, $except = NULL, 0);
if ($num_changed_sockets === false) {
/* Error handling */
} else if ($num_changed_sockets > 0) {
/* At least at one of the sockets something interesting happened */
}
?>
Nota: Devido a uma limitação no atual Zend Engine não é possível passar um modificador de constante como NULL diretamente como parâmetro para uma função que tem exceção para este parâmetro sendo passado por referência. Ao invés disso use uma variável temporária ou uma expressão com o leftmost member sendo uma variável temporária:
Exemplo #2 Usando NULL com socket_select()
<?php
socket_select($r, $w, $e = NULL, 0);
?>
O tv_sec e tv_usec juntos formam o parâmetro timeout. O timeout é o limite máximo da quantidade de tempo passado antes do retorno de socket_select(). tv_sec deve ser zero , causando o retorno imediato de socket_select(). Isso é útil para polling. Se tv_sec é NULL (sem timeout), socket_select() pode bloquear definitivamente.
Em caso de sucesso socket_select() retorna o número de socket resorces contidos nos arrays modificados, que deve ser zero se alguma coisa interessante acontecer antes do timeout expirar. Em caso de erro, FALSE é retornado. O código do erro pode ser retornado com socket_last_error().
Nota: Tenha certeza de usar o operador === quando checar por um erro. Desde de que socket_select() deve retornar 0 a comparação com == deve retornar TRUE:
Exemplo #3 Entendendo resultados de socket_select()
<?php
if (false === socket_select($r, $w, $e = NULL, 0)) {
echo "socket_select() failed, reason: " .
socket_strerror(socket_last_error()) . "\n";
}
?>
Nota: Esteja consciente que algumas implementações de sockets precisam ser manuseadas com muito cuidado. As regras básicas:
- Você deve sempre tentar usar socket_select() sem timeout. Seu programa não deve fazer nada se não há dados disponíveis. Códigos que dependem de timeouts não são usualmente portáveis e dificultam o debug.
- No socket resource deve ser adicionado alguma configuração se você não pretende checar o resultado após a chamada de socket_select(), e responder de forma apropriada. Após o retorno de socket_select(), todos os sockets resources em todos os arrays devem ser checados. Algum socket resource que está disponível para escrita deve ser escrito, e algum socket resource disponível para leitura deve ser lido.
- Se você está lendo/escrevendo um retorno de socket em array esteja consciente que eles não necessariamente estão lendo/escrevendo a quantidade completa de dados que você requisitou. Esteja preparado para somente habilitar para leitura/escrita um único byte.
- Isso é comum na grande maioria de implementações de socket que pegam exceções através do except array está fora do limite de dados recebidos no socket.
Veja também socket_read(), socket_write(), socket_last_error() e socket_strerror().
socket_select
07-Oct-2009 04:21
18-Mar-2009 12:06
Just noticed that you have to loop socket_select () when using UDP to get all queued packets:
<?php
while (socket_select ($aRead, $aWrite, $aExcept, 1) > 0) {
foreach ($aReadUdp as $oSocket) {
$this->clientReadUdp ($oSocket);
}
}
?>
That's important because every call of socket_select () on UDP brings you only one result. But there could be 10.000 results queued and if your turnarround time is to slow (server busy, other sleeps etc.), you'll never progress all results in near realtime.
16-Sep-2008 02:45
Just to add to this. Since the information contained in the notes is somewhat old. It appears keys are being preserved now.
So, if you rely on knowing which keys need to be worked with and were like me and thought that it didnot preserve. Well it does.
30-Nov-2007 07:38
In regards to the code posted by vardhan ( at ) rogers ( dot ) com, it appears that on the following line:
if (socket_select($read, $write = NULL, $except = NULL, 0) < 1)
the timeout parameter is accidentally set to 0, rather than NULL. This means that the select call will return immediately rather than blocking indefinitely.
Change the socket_select line to the following for great success:
if (socket_select($read, $write = NULL, $except = NULL, NULL) < 1)
If you want to use a simple fractional value for timeout:
<?php
socket_select(..., floor($timeout), ceil($timeout*1000000));
?>
20-Apr-2006 12:35
Another solution to the problem of keys not being preserved is to have an additional array for looking up sockets that uses their resource identifiers as keys. This can be obtained using array_flip() in some cases, but is particularly useful if each socket is associated with an object. In this case, you can make the object's constructor add a pointer to itself to the lookup array with its socket resource identifier as a key and use the following code to execute a read method for the object associated with each socket returned by socket_select():
<?php
socket_select($reads, $writes, $excepts, 0);
foreach ($sockets as $socket) {
$lookuparray[$socket]->read();
}
?>
01-Nov-2005 11:35
Regarding the comment below, No, it does not, it's a system call and I believe it's rather hard to preserve keys.
Additionally, socket_select should be used like it was a user-inputted array, that you don't know what you sent in to.
<?php
$reads = $clients;
$reads[] = $server;
socket_select($reads);
foreach ($reads as $read) {
/* do some stuff */
}
?>
30-Sep-2005 09:15
Note that the resulting arrays do NOT maintain keys (PHP 4.3.2) after being run through this function:
Before:
Array
(
[Client_Socket] => Resource id #6
[Server_Socket] => Resource id #9
)
After:
Array
(
[0] => Resource id #6
[1] => Resource id #9
)
It would have been nice to have the keys stay to figure out which stream you need to receive from, but you'll have to use some fancy foreach loop to figure out which sockets to check.
28-Aug-2005 12:46
A simple PHP script using socket_select() to manage multiple connections.
connect using "telnet localhost 9050". it broadcasts your messages that you send through telnet to other users connected to the server -- sort of like a chat script
#!/usr/local/bin/php
<?php
$port = 9050;
// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// set the option to reuse the port
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
// "bind" the socket to the address to "localhost", on port $port
// so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
socket_bind($sock, 0, $port);
// start listen for connections
socket_listen($sock);
// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);
while (true) {
// create a copy, so $clients doesn't get modified by socket_select()
$read = $clients;
// get a list of all the clients that have data to be read from
// if there are no clients with data, go to next iteration
if (socket_select($read, $write = NULL, $except = NULL, 0) < 1)
continue;
// check if there is a client trying to connect
if (in_array($sock, $read)) {
// accept the client, and add him to the $clients array
$clients[] = $newsock = socket_accept($sock);
// send the client a welcome message
socket_write($newsock, "no noobs, but ill make an exception :)\n".
"There are ".(count($clients) - 1)." client(s) connected to the server\n");
socket_getpeername($newsock, $ip);
echo "New client connected: {$ip}\n";
// remove the listening socket from the clients-with-data array
$key = array_search($sock, $read);
unset($read[$key]);
}
// loop through all the clients that have data to read from
foreach ($read as $read_sock) {
// read until newline or 1024 bytes
// socket_read while show errors when the client is disconnected, so silence the error messages
$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
// check if the client is disconnected
if ($data === false) {
// remove client for $clients array
$key = array_search($read_sock, $clients);
unset($clients[$key]);
echo "client disconnected.\n";
// continue to the next client to read from, if any
continue;
}
// trim off the trailing/beginning white spaces
$data = trim($data);
// check if there is any data after trimming off the spaces
if (!empty($data)) {
// send this to all the clients in the $clients array (except the first one, which is a listening socket)
foreach ($clients as $send_sock) {
// if its the listening sock or the client that we got the message from, go to the next one in the list
if ($send_sock == $sock || $send_sock == $read_sock)
continue;
// write the message to the client -- add a newline character to the end of the message
socket_write($send_sock, $data."\n");
} // end of broadcast foreach
}
} // end of reading foreach
}
// close the listening socket
socket_close($sock);
?>
01-May-2005 06:13
The continuation of my my previous post on 28-Apr-2005 10:19 at
http://ca3.php.net/manual/en/function.socket-select.php
Here it is: (Link is broken into 2 parts)
'http://gtkphp.org/php_socket_select_hangs
_explanation_and_solution.html'
16-Sep-2004 02:37
It is probably a bad idea to watch an array of sockets for input with socket_select, and then socket_read() using PHP_NORMAL_READ.
Although this seems desirable, you can end up with a permanently blocked program, if someone sends you malformed input which is missing a trailing \n \r. Guess how I found that out.
14-Feb-2003 10:36
Please note that the timeout parameter has important side-effects on the CPU usage of your script.
Setting the timeout to 0 will make your CPU looping without any time to have some rest and handle other running processes on your system, causing the system load to increase heavily while your script is running.
Personnaly, I use a value of 15 ms for this parameter. this ensures a good listening frequency while letting your system load clear.
Example :
$read = array($ListeningSocket);
$num_changed_sockets = socket_select($read, $write = NULL, $except = NULL, 0, 10);
Hope this helps.
09-Sep-2002 02:27
If you haven't done any network programming before, PHP's socket_select() might appear a bit strange to you. I've written a simple php "partyline" script to demonstrate the multi-socket use of select'ing at http://dave.dapond.com/socketselect.php.txt
09-Jul-2002 03:15
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:
<?php
$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
<?php
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;
}
}
?>
