socket_set_option

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

socket_set_optionSets socket options for the socket

Description

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.

Parameters

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.

Return Values

Returns true on success or false on failure.

Changelog

Version Description
8.0.0 socket is a Socket instance now; previously, it was a resource.

Examples

Example #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;
}
?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial