setsockopt

Sets options on a socket.

Format

#include  <socket.h>

int setsockopt  (int s, int level, int
                optname, char *optval, int
                optlen);

Arguments

s
A socket descriptor created by socket.
level
The protocol level for which the socket options are to be modified. It may have one of the following values:
SOL_SOCKET  Set the options at the socket level. 
Any protocol number. Set the options for protocol level p. See the <in.h> header file for the various IPPROTO values. 
optname
Is interpreted by the protocol specified in level. Options at each protocol level are documented with the protocol.

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.

optval
Points to a buffer containing the parameters of the specified option.

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.

optlen
An integer containing the size of the buffer pointed to by optval.

Description

This routine manipulates options associated with a socket. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.

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.

Return Values
Indicates success. 
-1  Indicates an error; errno is set to one of the following:

  • EBADF - The descriptor is invalid.

  • ENOTSOCK - The socket descriptor references a file, not a socket.

  • ENOTCONN - The socket is not connected.

  • ENOPROTOOPT - The option is unknown.

  • EFAULT - The optname parameter is not a valid part of the user address space.
 


Previous Page | Next Page | Table of Contents | Index