The setsockopt() and getsockopt() functions set values for various options of a socket or query their current values.
For example, you can set options for the following purposes:
- mark a socket for sending broadcast messages
cause a socket to wait until all data has been transferred before terminating the connection
The general form of the calls are as follows:
setsockopt(s, level, optname, optval, optlen); getsockopt(s, level, optname, optval, optlenp);
s
specifies the socket for which the option is to be set or queried.
level
specifies the protocol level to which the option belongs. Usually this is the socket level, indicated by the symbolic constant SOL_SOCKET which is defined in <sys/socket.h>. However, setting and querying options from other levels (IPPROTO_TCP, IPPROTO_IP and IPPROTO_IPV6) is also supported.
optname
specifies the socket option. The socket option is also a symbolic constant defined in <sys/socket.h>.
optval
is a pointer to the value of the option. The type of optval is different for the different options. With setsockopt(), you use optval to assign a value to the optname option for socket s. With getsockopt(), the value of the optname option for the socket s is output in optval.
optlen
specifies the length of the value of the option with setsockopt().
optlenp
is a pointer which, when getsockopt() is called, specifies the size of the memory area pointed to by optval. After getsockopt() returns, optlenp contains the actual length of the option value returned in this memory area.