The Windows Sockets setsockopt function sets a socket option.
int setsockopt (
SOCKET s,
int level,
int optname,
const char FAR * optval,
int optlen
);
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 | Meaning |
---|---|---|
SO_BROADCAST | BOOL | Allow transmission of broadcast messages on the socket. |
SO_DEBUG | BOOL | Record debugging information. |
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_OOBINLINE | BOOL | Receive out-of-band data in the normal data stream. (See section DECnet Out-Of-band data for a discussion of this topic.) |
SO_RCVBUF | int | Specify the total per-socket buffer space reserved for receives. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. |
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. |
PVD_CONFIG | Service Provider Dependent | This object stores the configuration information for the service provider associated with socket s. The exact format of this data structure is service provider specific. |
level = IPPROTO_TCP1
TCP_NODELAY | BOOL | Disables the Nagle algorithm for send coalescing. |
1 included for backward compatibility with Windows Sockets 1.1 |
BSD options not supported for setsockopt are:
Value | Type | Meaning |
---|---|---|
SO_ACCEPTCONN | BOOL | Socket is listening |
SO_RCVLOWAT | int | Receive low water mark |
SO_RCVTIMEO | int | Receive time-out (available in Microsoft implementation of Windows Sockets 2) |
SO_SNDLOWAT | int | Send low water mark |
SO_SNDTIMEO | int | Send time-out (available in Microsoft implementation of Windows Sockets 2) |
SO_TYPE | int | Type of the socket |
The WSAENOPROTOOPT error is indicated for nongroup sockets or for service providers that do not support group sockets.
Windows CE: The SO_RCVBUF option is not supported. If you attempt to use this option setsockopt returns WSAEOPNOTSUPP.
To set the 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. For example,
DWORD optval = SO_SEC_SSL;
err = setsockopt(
Socket,
SOL_SOCKET,
SO_SECURE,
&optval,
sizeof(optval)
);.
In addition to the normal error values, the setsockopt function can return an additional error code, namely, WSAEISCONN, to signify that the socket can not switch to secure mode once it has been connected.
When used in the context of SSL, the WSAENOPROTOOP error code acquires additional meaning, to indicate that the option level does not equal to SO_SOCKET.
For IrSocket implementation, Af_irda.h must be explicitly included.
The WSAENETDOWN return value is not supported for IrSockets.
IrSockets 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. It is expected that the application allocate a buffer of the necessary size for the passed parameters.
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.
If no error occurs, setsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
WSANOTINITIALISED | A successful WSAStartup must occur before using this function. |
WSAENETDOWN | The network subsystem has failed. |
WSAEFAULT | optval is not in a valid part of the process address space or optlen parameter is too small. |
WSAEINPROGRESS | A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. |
WSAEINVAL | level is not valid, or the information in optval is not valid. |
WSAENETRESET | 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). |
WSAENOTCONN | Connection has been reset when SO_KEEPALIVE is set. |
WSAENOTSOCK | The descriptor is not a socket. |
Windows NT: Yes
Windows: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.
bind, getsockopt, ioctlsocket, socket, WSAAsyncSelect, WSAEventSelect