getsockopt, setsockopt

getsockopt, setsockopt — get and set a socket option.

Syntax

getsockopt (socket, option)
setsockopt (socket, option, value1, value2? = nil)
    

Arguments

socket

The file descriptor of a socket.

option

The option being queried. Supported options and their possible values are listed below.

value

The 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.

Returns

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.

Description

These functions get and set a socket option, using the socket's file descriptor. The supported socket options are given below.

[Note]

SO_SNDTIMEO, SO_RCVTIMEO, SO_SNDLOWAT and SO_RCVLOWAT are not supported by all operating systems.

OptionPossible ValuesComments
SO_BROADCAST0 for off, non-zero for on.Allows for broadcasting datagrams from the socket.
SO_DEBUG0 for off, non-zero for on.Records debugging information.
SO_DONTROUTE0 for off, non-zero for on.Sends messages directly to the network interface instead of using normal message routing.
SO_ERRORA number.Resets the error status (for getsockopt only).
SO_KEEPALIVE0 for off, non-zero for on.Transmits messages periodically on a connected socket. No response means the connection is broken.
SO_LINGERTwo 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_NODELAY0 for enable, non-zero for disable.Disables the Nagle algorithm for sending data.
SO_OOBINLINE0 for off, non-zero for on.Puts out-of-band data in the normal input queue.
SO_REUSEADDR0 for off, non-zero for on.Permits the reuse of local addresses for this socket.
SO_RCVBUFA number.The size of the input buffer.
SO_RCVLOWATA number.Sets the minimum count for input operations.
SO_RCVTIMEO Two values: seconds and nanoseconds.Sets a timeout value for input.
SO_SNDBUFA number.The size of the output buffer.
SO_SNDLOWATA number.Sets the minimum count for output operations.
SO_SNDTIMEO Two values: seconds and nanoseconds.Sets a timeout value for output.
SO_TYPEA number.The type of socket (for getsockopt only).

Example

Gamma> skt = tcp_connect("localhost", 22);
8
Gamma> getsockopt(skt, SO_KEEPALIVE);
0
Gamma> setsockopt(skt, SO_KEEPALIVE, 1);
0
Gamma> getsockopt(skt, SO_KEEPALIVE);
1
Gamma> getsockopt(skt, SO_DEBUG);
0
Gamma> setsockopt(skt, SO_DEBUG, 1);
-1
Gamma> getsockopt(skt, SO_DEBUG);
0
Gamma>