getsockopt, setsockopt
getsockopt,
setsockopt — get and set a socket option.
getsockopt (socket, option)
setsockopt (socket, option, value1, value2? = nil)
socketThe file descriptor of a socket.
optionThe option being queried. Supported options and their possible values are listed below.
valueThe value to set the socket option to. There may be one or two values, depending on the option. If a socket option requires two values, both must be specified.
getsockopt returns the socket option value(s) on
success, as shown below, or nil on failure. When the option has two
values, they are returned as a list.
setsockopt returns 0 on success,
otherwise -1.
These functions get and set a socket option, using the socket's file descriptor. The supported socket options are given below.
![]() | |
|
| Option | Possible Values | Comments |
|---|---|---|
SO_BROADCAST | 0 for off, non-zero for
on. | Allows for broadcasting datagrams from the socket. |
SO_DEBUG | 0 for off, non-zero for
on. | Records debugging information. |
SO_DONTROUTE | 0 for off, non-zero for
on. | Sends messages directly to the network interface instead of using normal message routing. |
SO_ERROR | A number. | Resets the error status (for
getsockopt only). |
SO_KEEPALIVE | 0 for off, non-zero for
on. | Transmits messages periodically on a connected socket. No response means the connection is broken. |
SO_LINGER | Two values: on_or_off and
linger_time, where
on_or_off is 0
for off, non-zero for on. If on, a value for
linger_time is
required. | Keeps the socket open after a
close() call, to deliver
untransmitted messages. If on_or_off
is non-zero, the socket will block for the duration of
the linger_time or until all messages
have been sent. |
TCP_NODELAY | 0 for enable, non-zero for
disable. | Disables the Nagle algorithm for sending data. |
SO_OOBINLINE | 0 for off, non-zero for
on. | Puts out-of-band data in the normal input queue. |
SO_REUSEADDR | 0 for off, non-zero for
on. | Permits the reuse of local addresses for this socket. |
SO_RCVBUF | A number. | The size of the input buffer. |
SO_RCVLOWAT | A number. | Sets the minimum count for input operations. |
SO_RCVTIMEO | Two values: seconds and
nanoseconds. | Sets a timeout value for input. |
SO_SNDBUF | A number. | The size of the output buffer. |
SO_SNDLOWAT | A number. | Sets the minimum count for output operations. |
SO_SNDTIMEO | Two values: seconds and
nanoseconds. | Sets a timeout value for output. |
SO_TYPE | A number. | The type of socket (for
getsockopt only). |
Gamma>skt = tcp_connect("localhost", 22);8Gamma>getsockopt(skt, SO_KEEPALIVE);0Gamma>setsockopt(skt, SO_KEEPALIVE, 1);0Gamma>getsockopt(skt, SO_KEEPALIVE);1Gamma>getsockopt(skt, SO_DEBUG);0Gamma>setsockopt(skt, SO_DEBUG, 1);-1Gamma>getsockopt(skt, SO_DEBUG);0Gamma>