.TH setsockopt() "" "" "Sockets Function (libsocket)"
.PC "Set a socket option"
.B "#include <sys/types.h>"
.B "#include <sys/socket.h>"
\fBint setsockopt(\fIsocket\^\fB, \fIlevel\^\fB, \fIoption\^\fB, \fIbuffer\^\fB, \fIlength\^\fB)\fR
\fBint \fIsocket\^\fB, \fIlevel\^\fB, \fIoption\^\fB, \fIlength\^\fB;\fR
\fBchar *\fIbuffer\^\fB;\fR
.PP
Function
.B setsockopt()
sets options on a socket.
.PP
.I socket
gives the identifier of the socket, as returned by the function
.BR socket() .
.PP
.I level
gives the level at which the options are set.
To retrieve options set on the socket level, set
.I level
to
.B SOL_SOCKET ;
whereas to retrieve options set the TCP level, set
.I level
to the number of the TCP protocol.
.PP
.I option
gives the number of the option to set.
A list of options that are recognized at the socket level appears below.
Options at other levels are set by their respective protocols.
.PP
.I buffer
gives the address of the buffer that holds the option.
.I length
gives the length of
.IR buffer ,
in bytes.
.PP
The following options are recognized at the socket level.
They are set in header file
.BR <sys/socket.h> :
.IP \fBSO_BROADCAST\fR
Toggle permission to transmit broadcast messages.
.IP \fBSO_KEEPALIVE\fR
Toggle whether to keep a connection alive by periodically
transmitting messages.
If the connected party fails to respond to a message,
the connection is considered broken and processes that use the
socket are notified via the signal
.BR SIGPIPE .
.IP \fBSO_LINGER\fR
Control the action taken when a socket is closed but
contains unsent messages.
If
.B SO_LINGER
is set and the socket promises reliable delivery of data,
the system blocks the process that is attempting to close
.I socket
until
.I socket
can transmit its data or its attempts to do so time out.
If
.B SO_LINGER
is not enabled, the socket is closed immediately
and the unsent messages are thrown away.
.IP \fBSO_OOBINLINE\fR
Toggle whether a band can receive out-of-band data.
Such data can then be read by the function
.B recv()
or sent by the function
.BR send() ,
when invoked with the flag
.BR MSG_OOB .
.IP \fBSO_RCVBUF\fR
.IS \fBSO_SNDBUF\fR
Set the size of the receive or send buffer, respectively.
You can increase the size of a buffer to speed high-volume connections,
or decrease it to limit the amount of data that are backlogged.
The system places an absolute limit on these values.
.IP \fBSO_REUSEADDR\fR
Toggle whether local addresses can be reused.
.PP
If all goes well,
.B setsockopt()
returns zero.
If something goes wrong, it returns \-1 and set
.B errno
to one of the following values:
.IP \fBEBADF\fR
.I socket
does not identify a valid socket.
.IP \fBENOMEM\fR
The available user memory was insufficient to complete the operation.
.IP \fBENOPROTOOPT\fR
.I option
gives an unknown option.
.IP \fBENOTSOCK\fR
.I socket
identifies a file, not a socket.
.SH "See Also"
.Xr "getsockopt()," getsockop
.Xr "libsocket" libsocket
