Sets options on a socket.
#include <socket.h> int setsockopt (int s, int level, int optname, char *optval, int optlen);
SOL_SOCKET | Set the options at the socket level. |
p | Any protocol number. Set the options for protocol level p. See the <in.h> header file for the various IPPROTO values. |
The following options are available at the socket level:
SO_REUSEADDR indicates the rules used in validating addresses supplied in a bind call should allow reuse of local addresses.
SO_KEEPALIVE keeps connections alive by enabling the periodic transmission of messages on a connected socket. Should the connected party fail to respond to these messages, the connection is considered broken and processes using the socket are notified through an EPIPE error.
SO_DONTROUTE indicates that outgoing messages should bypass the standard routing facilities. Instead, messages are directed to the appropriate network interface according to the network portion of the destination address.
SO_LINGER delays the internal socket deletion portion of close until either the data has been transmitted, or the device times out (approximately eight minutes).
SO_BROADCAST is used to enable or disable broadcasting on the socket.
All socket level options other than SO_LINGER take an integer parameter that should be nonzero if the option is to be enabled, or zero if it is to be disabled.
SO_LINGER uses a linger structure parameter defined in the <socket.h> file. This structure specifies the desired state of the option and the linger interval. The option value for the SO_LINGER command is the address of a linger structure. See the <socket.h> header file for a description of the linger structure.
If the socket promises reliable delivery of data and l_onoff is nonzero, the system will block the process on the close attempt until it is able to transmit the data or until it decides it is unable to deliver the information. A timeout period, called the linger interval, is specified in l_linger.
If l_onoff is set to 0 and a close is issued, the system will process the close in a manner that allows the process to continue as quickly as possible.
When manipulating socket options, the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, specify level as SOL_SOCKET. To manipulate options at any other level, the protocol number of the appropriate protocol controlling the option must be supplied. For example, to indicate an option is to be interpreted by the TCP protocol, level should be set to the protocol number (IPPROTO_TCP) of TCP. See <in.h> for the various IPPROTO values.
0 | Indicates success. |
-1 | Indicates an error; errno is set to one
of the following:
|