Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

624 Network IPC: Sockets Chapter 16


We can set a socket option with thesetsockoptfunction.
#include <sys/socket.h>
int setsockopt(intsockfd,intlevel,int option,const void *val,
socklen_tlen);
Returns: 0 if OK,−1 on error
Thelevelargument identifies the protocol to which the option applies. If the option is a
generic socket-level option, thenlevelis set toSOL_SOCKET.Otherwise,levelis set to
the number of the protocol that controls the option. Examples areIPPROTO_TCPfor
TCP options andIPPROTO_IPfor IP options. Figure16.21 summarizes the generic
socket-level options defined by the Single UNIX Specification.

Option Type ofvalargument Description
SO_ACCEPTCONN int Return whether a socket is enabled for listening
(getsockoptonly).
SO_BROADCAST int Broadcast datagrams if*valis nonzero.
SO_DEBUG int Debugging in network drivers enabled if*valis nonzero.
SO_DONTROUTE int Bypass normal routing if*valis nonzero.
SO_ERROR int Return and clear pending socket error (getsockoptonly).
SO_KEEPALIVE int Periodic keep-alive messages enabled if*valis nonzero.
SO_LINGER struct linger Delay time when unsent messages exist and socket is closed.
SO_OOBINLINE int Out-of-band data placed inline with normal data if*valis
nonzero.
SO_RCVBUF int The size in bytes of the receive buffer.
SO_RCVLOWAT int The minimum amount of data in bytes to return on a receive
call.
SO_RCVTIMEO struct timeval The timeout value for a socket receive call.
SO_REUSEADDR int Reuse addresses inbindif*valis nonzero.
SO_SNDBUF int The size in bytes of the send buffer.
SO_SNDLOWAT int The minimum amount of data in bytes to transmit in a send
call.
SO_SNDTIMEO struct timeval The timeout value for a socket send call.
SO_TYPE int Identify the socket type (getsockoptonly).

Figure 16.21 Socket options

Thevalargument points to a data structure or an integer,depending on the option.
Some options areon/offswitches. If the integer is nonzero, then the option is enabled.
If the integer is zero, then the option is disabled. Thelenargument specifies the size of
the object to whichvalpoints.
We can find out the current value of an option with thegetsockoptfunction.
#include <sys/socket.h>
int getsockopt(intsockfd,int level,int option,void *restrictval,
socklen_t *restrictlenp);
Returns: 0 if OK,−1 on error
Free download pdf