socket_select
socket_select
Runs the select() system call on the given arrays of sockets with a specified timeout
**socket_select** $read $write $except $seconds int $microseconds
accepts arrays of sockets and waits for
them to change status. Those coming with BSD sockets background will
recognize that those socket arrays are in fact the so-called file
descriptor sets. Three independent arrays of sockets are watched.
socket_select
read
The sockets listed in the array will be
watched to see if characters become available for reading (more
precisely, to see if a read will not block - in particular, a socket
is also ready on end-of-file, in which case a
will return a zero length string).
read``socket_read
write
The sockets listed in the array will be
watched to see if a write will not block.
write
except
The sockets listed in the array will be
watched for exceptions.
except
seconds
The and
together form the parameter. The
is an upper bound on the amount of time
elapsed before return.
may be zero , causing
to return immediately. This is useful
for polling. If is null (no timeout),
can block indefinitely.
seconds``microseconds``timeout``timeout``socket_select``seconds``socket_select``seconds``socket_select
microseconds
Waarschuwing: > On exit, the arrays are modified to indicate which socket actually changed status.
You do not need to pass every array to
. You can leave it out and use an
empty array or null instead. Also do not forget that those arrays are
passed and will be modified after
returns.
socket_selectby referencesocket_select
Opmerking: > Due a limitation in the current Zend Engine it is not possible to pass a constant modifier like null directly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable:
**Voorbeeld: Using null with **
<?php $e = NULL; socket_select($r, $w, $e, 0); ?>
On success returns the number of
sockets contained in the modified arrays, which may be zero if
the timeout expires before anything interesting happens.On error false
is returned. The error code can be retrieved with
.
socket_select``socket_last_error
Opmerking: > Be sure to use the operator when checking for an error. Since the may return 0 the comparison with would evaluate to true:
===``socket_select``==Voorbeeld: Understanding 's result<?php $e = NULL; if (false === socket_select($r, $w, $e, 0)) { echo "socket_select() failed, reason: " . socket_strerror(socket_last_error()) . "\n"; } ?>
Voorbeeld: example
<?php
/* Prepare the read array */
$read = array($socket1, $socket2);
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, 0);
if ($num_changed_sockets === false) {
/* Error handling */
} else if ($num_changed_sockets > 0) {
/* At least at one of the sockets something interesting happened */
}
?>
Opmerking: > Be aware that some socket implementations need to be handled very carefully. A few basic rules:
- You should always try to use without timeout. Your program should have nothing to do if there is no data available. Code that depends on timeouts is not usually portable and difficult to debug.
socket_select- No socket must be added to any set if you do not intend to check its result after the call, and respond appropriately. After returns, all sockets in all arrays must be checked. Any socket that is available for writing must be written to, and any socket available for reading must be read from.
socket_select``socket_select- If you read/write to a socket returns in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte.
- It's common to most socket implementations that the only exception caught with the array is out-of-bound data received on a socket.
except
socket_read``socket_write``socket_last_error``socket_strerror