PHP. Руководство по PHP. 2005
socket_selectПред.
socket_select
(PHP4 >= 4.1.0, PHP 5)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])socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets 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.
The sockets listed in the read 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 resource is also ready on end-of-file, in which case a socket_read() will return a zero length string).
The sockets listed in the write array will be watched to see if a write will not block.
The sockets listed in the except array will be watched for exceptions.
Внимание |
- 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.
Замечание: 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:
Пример 2. Using NULL with socket_select()
|
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.
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().
Замечание: 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:
Пример 3. Understanding socket_select()'s result
|
Замечание: Be aware that some socket implementations need to be handled very carefully. A few basic rules:
See also socket_read(), socket_write(), socket_last_error() and socket_strerror().
socket_recvfrom
socket_send