Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

socket_select

Description

The socket_select of Sockets for PHP runs the select() system call on the given arrays of sockets with a specified timeout.

Syntax

socket_select(
    ?array &$read,
    ?array &$write,
    ?array &$except,
    ?int $seconds,
    int $microseconds = 0
): int|false

Parameters

read

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 is also ready 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 exceptions.

seconds

The seconds and microseconds together form the timeout parameter. The timeout is an upper bound on the amount of time elapsed before socket_select() return.

seconds may be zero, causing socket_select() to return immediately. This is useful for polling. If seconds is null (no timeout), socket_select() can block indefinitely.

microseconds

The seconds and microseconds together form the timeout parameter.

Return

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_last_error(). This code may be passed to socket_strerror() to get a textual representation of the 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

Examples

1 · read write except seconds

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket1 = socket_create($domain, $type, $protocol);

    if($socket1 === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $socket2 = socket_create($domain, $type, $protocol);

        if($socket2 === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $read = array($socket1, $socket2);
        $write = null;
        $except = null;
        $seconds = 0;

        $return = socket_select($read, $write, $except, $seconds);

        if($return === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_select: $socket_strerror");
        }

        var_export($return);

    socket_close($socket2);

socket_close($socket1);

?>
2

2 · microseconds

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket1 = socket_create($domain, $type, $protocol);

    if($socket1 === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $socket2 = socket_create($domain, $type, $protocol);

        if($socket2 === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_create: $socket_strerror");
        }

        $read = array($socket1, $socket2);
        $write = null;
        $except = null;
        $seconds = 0;
        $microseconds = 1;

        $return = socket_select($read, $write, $except, $seconds, $microseconds);

        if($return === false)
        {
            $error_code = socket_last_error();

            $socket_strerror = socket_strerror($error_code);

            die("socket_select: $socket_strerror");
        }

        var_export($return);

    socket_close($socket2);

socket_close($socket1);

?>
2
HomeMenu