= 4.3.0, PHP 5, PHP 7, PHP 8)socket_set_option — Sets socket options for the socket說(shuō)明socket_set_option( Socket $socket, ">

socket_set_option

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

socket_set_optionSets socket options for the socket

說(shuō)明

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.

參數(shù)

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 retrieve 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.

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。

更新日志

版本 說(shuō)明
8.0.0 現(xiàn)在 socketSocket 實(shí)例, 之前是 resource。

范例

示例 #1 socket_set_option() example

<?php
$socket 
socket_create(AF_INETSOCK_STREAMSOL_TCP);

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

if (!
socket_set_option($socketSOL_SOCKETSO_REUSEADDR1)) {
    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($socketSOL_SOCKETSO_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;
}
?>

參見(jiàn)