Network Interface Guide

Socket Options

You can set and get several options on sockets through setsockopt(3SOCKET) and getsockopt(3SOCKET); for example by changing the send or receive buffer space. The general forms of the calls are:

setsockopt(s, level, optname, optval, optlen);
getsockopt(s, level, optname, optval, optlen);

In some cases, such as setting the buffer sizes, these are only hints to the operating system. The operating system reserves the right to adjust the values appropriately.

Table 2-4 shows the arguments of the calls.

Table 2-4 setsockopt(3SOCKET) and getsockopt(3SOCKET) Arguments




Socket on which the option is to be applied 


Specifies the protocol level, such as socket level, indicated by the symbolic constant SOL_SOCKET in sys/socket.h


Symbolic constant defined in sys/socket.h that specifies the option


Points to the value of the option 


Points to the length of the value of the option 

For getsockopt(3SOCKET), optlen is a value-result argument, initially set to the size of the storage area pointed to by optval and set on return to the length of storage used.

It is sometimes useful to determine the type (for example, stream or datagram) of an existing socket. Programs invoked by inetd(1M) can do this by using the SO_TYPE socket option and the getsockopt(3SOCKET) 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 getsockopt(3SOCKET), type is set to the value of the socket type, as defined in sys/socket.h. For a datagram socket, type would be SOCK_DGRAM.