Socket Options
You can set and get several options on sockets through
setsockopt
()
and getsockopt
(). For
example, you can change the send or receive buffer space. For more
information, see the
setsockopt
(3C) and
getsockopt
(3C) man pages.
The general forms of the calls are in the following formats:
setsockopt(s, level, optname, optval, optlen); getsockopt(s, level, optname, optval, optlen);
The operating system can adjust the values appropriately at any time.
The arguments of setsockopt
and getsockopt
calls are in the following list:
- s
-
Socket on which the option is to be applied
- level
-
Specifies the protocol level, such as socket level, indicated by the symbolic constant
SOL_SOCKET
insys/socket.h
- optname
-
Symbolic constant defined in
sys/socket.h
that specifies the option - optval
-
Points to the value of the option
- optlen
-
Points to the length of the value of the option
For getsockopt
, optlen is a
value-result argument. This argument is initially set to the size of the storage
area pointed to by optval. On return, the argument's
value is set to the length of storage used.
When a program needs to determine an existing socket's type,
the program should invoke inetd
by 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 getsockopt
, 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
.