Socket Options
This section describes socket options that can be used with the setsockopt
and getsockopt
functions to configure socket behavior.
Overview
Socket options allow for fine-grained control over how a socket operates. They can affect aspects such as buffer sizes, keep-alive behavior, broadcast capabilities, and more. Each option is identified by a level and a name, and takes a value of a specific type.
Common Socket Options
The following table lists some commonly used socket options:
Option Name | Level | Description | Type |
---|---|---|---|
SO_BROADCAST |
SOL_SOCKET |
Enables or disables the use of the SO_BROADCAST socket option. | BOOL |
SO_DEBUG |
SOL_SOCKET |
Enables or disables socket debugging. | BOOL |
SO_ERROR |
SOL_SOCKET |
Retrieves pending error on the socket. | int |
SO_KEEPALIVE |
SOL_SOCKET |
Enables or disables the keep-alive socket option. | BOOL |
SO_LINGER |
SOL_SOCKET |
Specifies the time-out value for sending or receiving data when a socket is closed. | struct linger |
SO_RCVBUF |
SOL_SOCKET |
Sets or retrieves the size of the receive buffer. | int |
SO_SNDBUF |
SOL_SOCKET |
Sets or retrieves the size of the send buffer. | int |
SO_REUSEADDR |
SOL_SOCKET |
Indicates whether the bind function can associate a socket with a reserved port on the computer. |
BOOL |
IP_TTL |
IPPROTO_IP |
Sets or retrieves the time-to-live (TTL) value for outgoing IP packets. | int |
TCP_NODELAY |
IPPROTO_TCP |
Disables the Nagle algorithm. | BOOL |
Using setsockopt
The setsockopt
function is used to set a socket option. Its signature is:
int setsockopt(
SOCKET s,
int level,
int optname,
const char *optval,
int optlen
);
s
: A descriptor identifying the socket.level
: The level at which the option is defined (e.g.,SOL_SOCKET
for socket-level options,IPPROTO_IP
for IP-level options).optname
: The specific socket option to set.optval
: A pointer to the buffer that contains the value for the option.optlen
: The size, in bytes, of the buffer pointed to byoptval
.
Example: Enabling SO_REUSEADDR
This example demonstrates how to enable the SO_REUSEADDR
option to allow the socket to be bound to an address that is already in use.
#include <winsock2.h>
// Assume 'listenSocket' is a valid SOCKET descriptor
BOOL reuseAddr = TRUE;
int optLen = sizeof(BOOL);
if (setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseAddr, optLen) == SOCKET_ERROR) {
// Handle error
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed with error: %d\n", WSAGetLastError());
} else {
printf("SO_REUSEADDR enabled successfully.\n");
}
Using getsockopt
The getsockopt
function is used to retrieve the current value for a specified socket option. Its signature is:
int getsockopt(
SOCKET s,
int level,
int optname,
char *optval,
int *optlen
);
s
: A descriptor identifying the socket.level
: The level at which the option is defined.optname
: The specific socket option to retrieve.optval
: A pointer to the buffer that receives the value for the option.optlen
: A pointer to a buffer that contains the size, in bytes, of the buffer pointed to byoptval
, and on return, it contains the actual size of the value returned.
Example: Getting SO_RCVBUF size
This example shows how to retrieve the current receive buffer size for a socket.
#include <winsock2.h>
// Assume 'recvSocket' is a valid SOCKET descriptor
int rcvBufSize;
int optLen = sizeof(int);
if (getsockopt(recvSocket, SOL_SOCKET, SO_RCVBUF, (char*)&rcvBufSize, &optLen) == SOCKET_ERROR) {
// Handle error
fprintf(stderr, "getsockopt(SO_RCVBUF) failed with error: %d\n", WSAGetLastError());
} else {
printf("Current receive buffer size: %d bytes.\n", rcvBufSize);
}