7.2 getsockopt and setsockopt Functions
These two functions apply only to sockets.
#include <sys/socket.h> |
int getsockopt(int sockfd , int level , int optname , void *optval , socklen_t *optlen ); |
int setsockopt(int sockfd , int level , int optname , const void *optval socklen_t optlen ); |
Both return: 0 if OK,1 on error |
Figure 7.1. Summary of socket and IP-layer socket options for getsockopt and setsockopt.

Figure 7.2. Summary of transport-layer socket options.

There are two basic types of options: binary options that enable or disable a certain feature (flags), and options that fetch and return specific values that we can either set or examine (values). The column labeled "Flag" specifies if the option is a flag option. When calling getsockopt for these flag options, *optval is an integer. The value returned in *optval is zero if the option is disabled, or nonzero if the option is enabled. Similarly, setsockopt requires a nonzero *optval to turn the option on, and a zero value to turn the option off. If the "Flag" column does not contain a "•," then the option is used to pass a value of the specified datatype between the user process and the system.Subsequent sections of this chapter will give additional details on the options that affect a socket.