socket_set_option
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
socket_set_option — Sets socket options for the socket
Description
The socket_set_option() function sets the option
specified by the optname
parameter, at the
specified protocol level
, to the value pointed to
by the optval
parameter for the
socket
.
Parameters
-
socket
-
A valid socket resource 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, alevel
parameter ofSOL_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. -
optname
-
The available socket options are the same as those for the socket_get_option() function.
-
optval
-
The option value.
Examples
Example #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;
}
?>
See Also
- socket_create() - Create a socket (endpoint for communication)
- socket_bind() - Binds a name to a socket
- socket_strerror() - Return a string describing a socket error
- socket_last_error() - Returns the last error on the socket
- socket_get_option() - Gets socket options for the socket
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.socket-set-option.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.