DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with sockets

Socket options

It is possible to set and get several options on sockets via the setsockopt and getsockopt system calls. These options include such things as marking a socket for broadcasting, not to route, to linger on close, and so on. The general forms of the calls are:

   setsockopt(s, level, optname, optval, optlen);
and
   getsockopt(s, level, optname, optval, optlen);
The parameters to the calls are as follows: s is the socket on which the option is to be applied. level specifies the protocol layer on which the option is to be applied; usually this is the ``socket level,'' indicated by the symbolic constant SOL_SOCKET, defined in sys/socket.h. The option is specified in optname, and is a symbolic constant also defined in sys/socket.h. optval and optlen point to the value of the option (usually, whether the option is to be turned on or off), and the length of the value of the option, respectively. For getsockopt, optlen is a value-result parameter, initially set to the size of the storage area pointed to by optval, and modified on return to show the amount of storage used.

An example should help clarify things. It is sometimes useful to determine the type (for example, stream, datagram, and so on) of an existing socket; programs invoked by inetd (described below) may need to do this task using the SO_TYPE socket option and the getsockopt call:

   #include <sys/types.h>
   #include <sys/socket.h>
   

int type, size;

size = sizeof(int);

if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char *) &type, &size) < 0) { ... }

After the getsockopt call, type will be set to the value of the socket type, as defined in sys/socket.h. If, for example, the socket were a datagram socket, type would have the value corresponding to SOCK_DGRAM.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004