setsockopt(3) - Linux man page
Prolog
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of thisinterface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
Name
setsockopt - set the socket options
Synopsis
#include <sys/socket.h>
int setsockopt(int socket, int level, int option_name,
const void *option_value,socklen_t option_len);
Description
The setsockopt() function shall set the option specified by the option_name argument, at the protocol level specified by the levelargument, to the value pointed to by the option_value argument for the socket associated with the file descriptor specified by the socketargument.
The level argument specifies the protocol level at which the option resides. To set options at the socket level, specify the level argument asSOL_SOCKET. To set options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicatethat an option is interpreted by the TCP (Transport Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header.
The option_name argument specifies a single option to set. The option_name argument and any specified options are passed uninterpreted to theappropriate protocol module for interpretations. The <sys/socket.h> header defines the socket-level options. The options are as follows:
- SO_DEBUG
- Turns on recording of debugging information. This option enables or disables debugging in the underlying protocol modules. This option takes an intvalue. This is a Boolean option.
- SO_BROADCAST
- Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int value. This is a Boolean option.
- SO_REUSEADDR
- Specifies that the rules used in validating addresses supplied to bind() should allow reuse of local addresses, if this is supported by theprotocol. This option takes an int value. This is a Boolean option.
- SO_KEEPALIVE
- Keeps connections active by enabling the periodic transmission of messages, if this is supported by the protocol. This option takes an intvalue.
If the connected socket fails to respond to these messages, the connection is broken and threads writing to that socket are notified with a SIGPIPE signal.This is a Boolean option.
- SO_LINGER
- Lingers on a close() if data is present. This option controls the action taken when unsent messages queue on a socket and close() isperformed. If SO_LINGER is set, the system shall block the process during close() until it can transmit the data or until the time expires. If SO_LINGERis not specified, and close() is issued, the system handles the call in a way that allows the process to continue as quickly as possible. This optiontakes a linger structure, as defined in the <sys/socket.h> header, to specify the state of the option and linger interval.
- SO_OOBINLINE
- Leaves received out-of-band data (data marked urgent) inline. This option takes an int value. This is a Boolean option.
- SO_SNDBUF
- Sets send buffer size. This option takes an int value.
- SO_RCVBUF
- Sets receive buffer size. This option takes an int value.
- SO_DONTROUTE
- Requests that outgoing messages bypass the standard routing facilities. The destination shall be on a directly-connected network, and messages are directedto the appropriate network interface according to the destination address. The effect, if any, of this option depends on what protocol is in use. This optiontakes an int value. This is a Boolean option.
- SO_RCVLOWAT
- Sets the minimum number of bytes to process for socket input operations. The default value for SO_RCVLOWAT is 1. If SO_RCVLOWAT is set to a larger value,blocking receive calls normally wait until they have received the smaller of the low water mark value or the requested amount. (They may return less than thelow water mark if an error occurs, a signal is caught, or the type of data next in the receive queue is different from that returned; for example, out-of-banddata.) This option takes an int value. Note that not all implementations allow this option to be set.
- SO_RCVTIMEO
- Sets the timeout value that specifies the maximum amount of time an input function waits until it completes. It accepts a timeval structure with thenumber of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for thismuch time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data is received. Thedefault for this option is zero, which indicates that a receive operation shall not time out. This option takes a timeval structure. Note that not allimplementations allow this option to be set.
- SO_SNDLOWAT
- Sets the minimum number of bytes to process for socket output operations. Non-blocking output operations shall process no data if flow control does notallow the smaller of the send low water mark value or the entire request to be processed. This option takes an int value. Note that not allimplementations allow this option to be set.
- SO_SNDTIMEO
- Sets the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. If a send operationhas blocked for this time, it shall return with a partial count or with errno set to [EAGAIN] or [EWOULDBLOCK] if no data is sent. The default for thisoption is zero, which indicates that a send operation shall not time out. This option stores a timeval structure. Note that not all implementationsallow this option to be set.
For Boolean options, 0 indicates that the option is disabled and 1 indicates that the option is enabled.
Options at other protocol levels vary in format and name.
Return Value
Upon successful completion, setsockopt() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error.
Errors
The setsockopt() function shall fail if:
- EBADF
- The socket argument is not a valid file descriptor.
- EDOM
- The send and receive timeout values are too big to fit into the timeout fields in the socket structure.
- EINVAL
- The specified option is invalid at the specified socket level or the socket has been shut down.
- EISCONN
- The socket is already connected, and a specified option cannot be set while the socket is connected.
- ENOPROTOOPT
The option is not supported by the protocol.
- ENOTSOCK
- The socket argument does not refer to a socket.
The setsockopt() function may fail if:
- ENOMEM
- There was insufficient memory available for the operation to complete.
- ENOBUFS
- Insufficient resources are available in the system to complete the call.
The following sections are informative.
Examples
None.
Application Usage
The setsockopt() function provides an application program with the means to control socket behavior. An application program can usesetsockopt() to allocate buffer space, control timeouts, or permit socket data broadcasts. The <sys/socket.h> header defines thesocket-level options available to setsockopt().
Options may exist at multiple protocol levels. The SO_ options are always present at the uppermost socket level.
Rationale
None.
Future Directions
None.
See Also
Sockets, bind(), endprotoent(), getsockopt(), socket(), the Base Definitions volume of IEEE Std 1003.1-2001,<netinet/in.h>, <sys/socket.h>
Copyright
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1,2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright ?2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and theoriginal IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained onlineat
http://www.opengroup.org/unix/online.html .
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。