Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 16.6 Socket Options 625


Thelenpargument is a pointer to an integer.Beforecallinggetsockopt, we set the
integer to the size of the buffer wherethe option is to be copied. If the actual size of the
option is greater than this size, the option is silently truncated. If the actual size of the
option is less than this size, then the integer is updated with the actual size on return.

Example


The function in Figure16.12 fails to operate properly when the server terminates and
we try to restart it immediately.Normally,the implementation of TCP will prevent us
from binding the same address until a timeout expires, which is usually on the order of
several minutes. Luckily,theSO_REUSEADDRsocket option allows us to bypass this
restriction, as illustrated in Figure16.22.
#include "apue.h"
#include <errno.h>
#include <sys/socket.h>

int
initserver(int type, const struct sockaddr *addr, socklen_t alen,
int qlen)
{
int fd, err;
int reuse = 1;

if ((fd = socket(addr->sa_family, type, 0)) < 0)
return(-1);
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(int)) < 0)
goto errout;
if (bind(fd, addr, alen) < 0)
goto errout;
if (type == SOCK_STREAM || type == SOCK_SEQPACKET)
if (listen(fd, qlen) < 0)
goto errout;
return(fd);

errout:
err = errno;
close(fd);
errno = err;
return(-1);
}

Figure 16.22 Initialize a socket endpoint for use by a server with address reuse

To enable theSO_REUSEADDRoption, we set an integer to a nonzerovalue and pass the
address of the integer as thevalargument tosetsockopt.Weset thelenargument to
the size of an integer to indicate the size of the object to whichvalpoints.
Free download pdf