array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.socket-set-option.php', 1 => 'socket_set_option', 2 => 'Sets socket options for the socket', ), 'up' => array ( 0 => 'ref.sockets.php', 1 => 'Socket Функції', ), 'prev' => array ( 0 => 'function.socket-set-nonblock.php', 1 => 'socket_set_nonblock', ), 'next' => array ( 0 => 'function.socket-setopt.php', 1 => 'socket_setopt', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/sockets/functions/socket-set-option.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

socket_set_option

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

socket_set_optionSets socket options for the socket

Опис

function socket_set_option(
    Socket $socket,
    int $level,
    int $option,
    array|string|int $value
): bool

The socket_set_option() function sets the option specified by the option parameter, at the specified protocol level, to the value pointed to by the value parameter for the socket.

Параметри

socket

A Socket instance created with socket_create() or socket_accept().

level

The level parameter specifies the protocol level at which the option resides. For example, to set options at the socket level, a level parameter of SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname() function.

option

The available socket options are the same as those for the socket_get_option() function.

value

The option value.

Значення, що повертаються

Повертає true у разі успіху або false в разі помилки.

Журнал змін

Версія Опис
8.5.0 Now throws an exception when MCAST_LEAVE_GROUP or MCAST_LEAVE_SOURCE_GROUP is used and the value is not a valid object or array, and throws a ValueError when a multicast option is used on a socket that is not of AF_INET or AF_INET6 family.
8.0.0 Тепер socket є примірником Socket; раніше це був resource.

Приклади

Приклад #1 socket_set_option() example

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!is_resource($socket)) {
    echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}

if (!socket_bind($socket, '127.0.0.1', 1223)) {
    echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}

$rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);

if ($rval === false) {
    echo 'Unable to get socket option: '. socket_strerror(socket_last_error()) . PHP_EOL;
} else if ($rval !== 0) {
    echo 'SO_REUSEADDR is set on socket !' . PHP_EOL;
}
?>

Прогляньте також