socket_select
socket_select
(PHP 4 >= 4.1.0, PHP 5, PHP 7)
socket_select — Runs the select() system call on the given arrays of sockets with a specified timeout
Description
int socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] )
socket
_
select()
accepts arrays of socket
s and waits for them to change status. Those coming with BSD socket
s background will recognize that those socket
resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket
resources are watched.
Parameters
read
The sockets listed in the read
array will be watched to see if characters become available for read
ing (more precisely, to see if a read
will not block - in particular, a socket resource is also read
y on end-of-file, in which case a socket_read
() will return a zero length string).
write
The sockets listed in the write
array will be watched to see if a write
will not block.
except
The sockets listed in the except
array will be watched for except
ions.
tv_sec
The tv_sec
and tv_usec
together form the timeout
parameter. The timeout
is an upper bound on the amount of time elapsed before socket
_
select()
return. tv_sec
may be zero , causing socket
_
select()
to return immediately. This is useful for polling. If tv_sec
is NULL
(no timeout
), socket
_
select()
can block indefinitely.
tv_usec
Warning
On exit, the arrays are modified to indicate which socket resource actually changed status.
You do not need to pass every array to socket
_
select()
. You can leave it out and use an empty array or NULL
instead. Also do not forget that those arrays are passed by reference
and will be modified after socket
_
select()
returns.
Note: 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: Example #1 Using NULL with socket_select() <?php $e = NULL; socket\_select($r, $w, $e, 0 ?>
Return Values
On success socket
_
select()
returns the number of socket
resources 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
_
last_
error().
Note: Be sure to use the === operator when checking for an error. Since the socket_select() may return 0 the comparison with == would evaluate to TRUE: Example #2 Understanding socket_select()'s result <?php $e = NULL; if (false === socket\_select($r, $w, $e, 0)) { echo "socket\_select() failed, reason: " . socket\_strerror(socket\_last\_error()) . "\n"; } ?>
Examples
Example #3 socket
_
select() 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 */
}
?>
Notes
Note
: Be aware that some socket implementations need to be handled very carefully. A few basic rules:
- You should always try to use
socket
_
select()
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.
- No
socket
resource must be added to any set if you do not intend to check its result after thesocket
_
select()
call, and respond appropriately. Aftersocket
_
select()
returns, allsocket
resources in all arrays must be checked. Anysocket
resource that is available for writing must be written to, and anysocket
resource available for reading must be read from.
- 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
except
ion caught with theexcept
array is out-of-bound data received on a socket.
See Also
- socket_read() - Reads a maximum of length bytes from a socket
- socket_write() - Write to a socket
- socket_last_error() - Returns the last error on the socket
- socket_strerror() - Return a string describing a socket error
← socket_recvmsg
socket_send →
© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.