This function sets a socket option.
At a Glance
Header file: | Winsock.h |
Windows CE versions: | 1.0 and later |
Syntax
int setsockopt (SOCKET s, int level, int optname, const char *optval, int optlen);
Parameters
s
[in] Descriptor that identifies a socket.
level
[in] Level at which the option is defined; the supported levels include SOL_SOCKET, IPPROTO_TCP, and IPPROTO_IP. When the level is IPPROTO_IP, the following IP_OPTIONS values are available:
Value | Description |
IP_MULTICAST_TTL | Time-to-live for a multicast packet. Specifies the maximum number of hops for which a multicast message survives before it is discarded. Use int for optval. |
IP_MULTICAST_IF | Address of the outgoing multicast-capable interface. This address is used for subsequent multicasts. Use unsigned long for optval. |
IP_ADD_MEMBERSHIP | The membership request structure used to request a particular multicast group's membership. Use struct ip_mreq for optval. |
IP_DROP_MEMBERSHIP | The group whose membership is to be used. Use struct ip_mreq for optval. |
optname
[in] Socket option for which the value is to be set.
optval
[in] Pointer to the buffer in which the value for the requested option is supplied.
optlen
[in] Size of the optval buffer.
Return Values
Zero indicates that no error occurred. SOCKET_ERROR indicates failure. To get a specific error value, call WSAGetLastError.
The setsockopt function can also return the following error values:
WSAENOBUFS
No buffer space available. An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
WSAEOPNOTSUPP
The option is not supported.
WSANOTINITIALISED
A successful WSAStartup call must occur before using this function.
WSAENETDOWN
The network subsystem has failed. This value is not supported for IrSockets.
WSAEFAULT
The optval parameter is not in a valid part of the process address space or the optlen parameter is too small.
WSAEISCONN
The socket could not switch to secure mode when it connected.
WSAEINPROGRESS
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEINVAL
The level parameter is not valid, or the information in optval is not valid.
WSAENETRESET
The connection has timed out when SO_KEEPALIVE is set.
WSAENOPROTOOPT
The option is unknown or unsupported for the specified provider or socket (see SO_GROUP_PRIORITY limitations). When used in the context of SSL, this error value indicates that the option level is not equal to SO_SOCKET.
WSAENOTCONN
The connection has been reset when SO_KEEPALIVE is set.
WSAENOTSOCK
The descriptor is not a socket.
Remarks
The setsockopt function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost “socket” level. Options affect socket operations, such as whether expedited data (OOB data for example) is received in the normal data stream, and whether broadcast messages can be sent on the socket.
There are two types of socket options: Boolean options that enable or disable a feature or behavior, and options that require an integer value or structure. To enable a Boolean option, optval points to a nonzero integer. To disable the option optval points to an integer equal to zero. The optlen parameter should be equal to sizeof(int) for Boolean options. For other options, optval points to the an integer or structure that contains the desired value for the option, and optlen is the length of the integer or structure.
The following options are supported for setsockopt. For default values of these options, see the description. The Type identifies the type of data addressed by optval.
level = SOL_SOCKET
Value | Type | Description |
SO_BROADCAST | BOOL | Allow transmission of broadcast messages on the socket. |
SO_DONTLINGER | BOOL | Do not block close waiting for unsent data to be sent. Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. |
SO_DONTROUTE | BOOL | Do not route: send directly to interface. |
SO_GROUP_PRIORITY | int | Reserved for future use with socket groups. Specify the relative priority to be established for sockets that are part of a socket group. |
SO_KEEPALIVE | BOOL | Send keepalives |
SO_LINGER | struct LINGER | Linger on close if unsent data is present. |
SO_REUSEADDR | BOOL | Allow the socket to be bound to an address that is already in use. (See bind.) |
SO_SNDBUF | int | Specify the total per-socket buffer space reserved for sends. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. |
level = IPPROTO_TCP
Value | Type | Description |
TCP_NODELAY | BOOL | Disables the Nagle algorithm for send coalescing. |
BSD options not supported for setsockopt are:
Value | Type | Meaning |
SO_ACCEPTCONN | BOOL | Socket is listening |
SO_RCVLOWAT | int | Receive low water mark |
SO_SNDLOWAT | int | Send low water mark |
SO_SNDTIMEO | int | Send time-out |
SO_TYPE | int | Type of the socket |
SO_GROUP_PRIORITY
Reserved for future use with socket groups. Group priority indicates the relative priority of the specified socket relative to other sockets within the socket group. Values are non-negative integers, with zero corresponding to the highest priority. Priority values represent a hint to the underlying service provider about how potentially scarce resources should be allocated. For example, whenever two or more sockets are both ready to transmit data, the highest priority socket (lowest value for SO_GROUP_PRIORITY) should be serviced first with the remainder serviced in turn according to their relative priorities.
The WSAENOPROTOOPT error is indicated for nongroup sockets or for service providers that do not support group sockets.
SO_KEEPALIVE
An application can request that a TCP/IP provider enable the use of "keep-alive" packets on TCP connections by turning on the SO_KEEPALIVE socket option. A Windows Sockets provider need not support the use of keep-alives. If it does, the precise semantics are implementation-specific but should conform to section 4.2.3.6 of RFC 1122: Requirements for Internet Hosts—Communication Layers. If a connection is dropped as the result of "keep-alives" the error value WSAENETRESET is returned to any calls in progress on the socket, and any subsequent calls will fail with WSAENOTCONN.
SO_LINGER
The SO_LINGER option controls the action taken when unsent data is queued on a socket and a closesocket is performed. See closesocket for a description of the way in which the SO_LINGER settings affect the semantics of closesocket. The application sets the desired behavior by creating a LINGER structure (pointed to by the optval parameter) with these members l_onoff and l_linger set appropriately.
SO_REUSEADDR
By default, a socket cannot be bound (see bind) to a local address that is already in use. On occasion, however, it can be necessary to "re-use" an address in this way. Since every connection is uniquely identified by the combination of local and remote addresses, there is no problem with having two sockets bound to the same local address as long as the remote addresses are different. To inform the Windows Sockets provider that a bind on a socket should not be disallowed because the desired address is already in use by another socket, the application should set the SO_REUSEADDR socket option for the socket before issuing the bind. The option is interpreted only at the time of the bind. It is therefore unnecessary and harmless to set the option on a socket that is not to be bound to an existing address. Setting or resetting the option after the bind has no effect on this or any other socket.
SO_SNDBUF
When a Windows Sockets implementation supports the SO_SNDBUF option, an application can request different buffer sizes (larger or smaller). The call to setsockopt can succeed even when the implementation did not provide the whole amount requested. An application must call getsockopt with the same option to check the buffer size actually provided.
TCP_NODELAY
The TCP_NODELAY option is specific to TCP/IP service providers. The Nagle algorithm is disabled if the TCP_NODELAY option is enabled (and vice versa). The process involves buffering send data when there is unacknowledged data already "in flight" or buffering send data until a full-size packet can be sent. It is highly recommended that TCP/IP service providers enable the Nagle Algorithm by default, and for the vast majority of application protocols the Nagle Algorithm can deliver significant performance enhancements. However, for some applications this algorithm can impede performance, and TCP_NODELAY can be used to turn it off. These are applications where many small messages are sent, and the time delays between the messages are maintained. Application writers should not set TCP_NODELAY unless the impact of doing so is well-understood and desired because setting TCP_NODELAY can have a significant negative impact on network and application performance.
To set a socket to secure mode, the option level parameter, level, must set to SO_SOCKET, the option name, optname to SO_SECURE, and the option value, optval, must be a pointer to a DWORD containing SO_SEC_SSL. These settings ensure that the Unified Secure Sockets Layer (SSL) package be used. The following code example shows how to set a socket to secure mode.
DWORD optval = SO_SEC_SSL;
err = setsockopt(
Socket,
SOL_SOCKET,
SO_SECURE,
&optval,
sizeof(optval)
);.
For Infrared Sockets (IrSock), Af_irda.h must be explicitly included.
IrSock provides two settable socket options:
Value | Type | Meaning |
IRLMP_IAS_SET | * IAS_SET | Sets IAS attributes. |
IRLMP_IRLPT_MODE | * int | In non-zero, enables IrLPT mode for printing to IrDA printers. |
The IRLMP_IAS_SET socket option allows the application to set a single attribute of a single class in the local IAS. The application specifies the class to set and the attribute and attribute type. The maximum length of an attribute name is 60 characters however, regardless of the set value for characters beyond the 60th, Windows CE will set these to NULL. That is, these two arrays will be truncated at 60 characters.
The IRLMP_RAW_MODE socket option allows the application to switch between TinyTP mode and unreliable IrLMP mode. If it is not set, IrSockets are assumed to use TinyTP. This option is only available after issuing the socket function and before issuing any other Windows Sockets function.
Many SO_ level socket options are not meaningful to IrSockets. Only SO_LINGER is specifically supported.
See Also