歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux系統編程之getsockopt/setsockopt 函數

Linux系統編程之getsockopt/setsockopt 函數

日期:2017/3/1 10:14:26   编辑:Linux編程

最近看別人寫的代碼很多函數不知道啊,在研究分布式消息隊列beanstalkd,遇到了很多東西。尤其是網絡連接方面。

代碼是這樣的。

  1. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof flags);
  2. setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof flags);
  3. setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof linger);
  4. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof flags);

通過函數名,我們可以曉得,setsockopt 就是設這sock的一些選項,那這些參數都是什麼意義呢?

下面就是在網上收集的一些這兩個函數的東西。

通過英文文檔,我們可以知道

NAME
setsockopt - set the socket options
SYNOPSIS
#include <sys/socket.h>

int setsockopt(int socket, int level, int option_name,
const void *option_value, socklen_t option_len);

功能描述

功能描述:
獲取或者設置與某個套接字關聯的選 項。選項可能存在於多層協議中,它們總會出現在最上面的套接字層。當操作套接字選項時,選項位於的層和選項的名稱必須給出。為了操作套接字層的選項,應該 將層的值指定為SOL_SOCKET。為了操作其它層的選項,控制選項的合適協議號必須給出。例如,為了表示一個選項由TCP協議解析,層應該設定為協議 號TCP。

參數:
sock:將要被設置或者獲取選項的套接字。
level:選項所在的協議層。
optname:需要訪問的選項名。
optval:對於getsockopt(),指向返回選項值的緩沖。對於setsockopt(),指向包含新選項值的緩沖。
optlen:對於getsockopt(),作為入口參數時,選項值的最大長度。作為出口參數時,選項值的實際長度。對於setsockopt(),現選項的長度。 【Linux公社 http://www.linuxidc.com 】

下面有很多功能選項。

我就不翻譯了,原汁原味的好。

SO_DEBUG
Turns on recording of debugging information. This option enables or disables debugging in the underlying protocol modules. This option takes an int value. This is a Boolean option.
SO_BROADCAST
Permits sending of broadcast messages, if this is supported by the protocol. This option takes an int value. This is a Boolean option.
SO_REUSEADDR
Specifies that the rules used in validating addresses supplied to bind() should allow reuse of local addresses, if this is supported by the protocol. This option takes an intvalue. This is a Boolean option.
SO_KEEPALIVE
Keeps connections active by enabling the periodic transmission of messages, if this is supported by the protocol. This option takes an int value.
If the connected socket fails to respond to these messages, the connection is broken and threads writing to that socket are notified with a SIGPIPE signal. This is a Boolean option.

SO_LINGER
Lingers on a close() if data is present. This option controls the action taken when unsent messages queue on a socket and close() is performed. If SO_LINGER is set, the system shall block the calling thread during close() until it can transmit the data or until the time expires. If SO_LINGER is not specified, and close() is issued, the system handles the call in a way that allows the calling thread to continue as quickly as possible. This option takes a linger structure, as defined in the <sys/socket.h> header, to specify the state of the option and linger interval.
SO_OOBINLINE
Leaves received out-of-band data (data marked urgent) inline. This option takes an int value. This is a Boolean option.
SO_SNDBUF
Sets send buffer size. This option takes an int value.
SO_RCVBUF
Sets receive buffer size. This option takes an int value.
SO_DONTROUTE
Requests that outgoing messages bypass the standard routing facilities. The destination shall be on a directly-connected network, and messages are directed to the appropriate network interface according to the destination address. The effect, if any, of this option depends on what protocol is in use. This option takes an int value. This is a Boolean option.
SO_RCVLOWAT
Sets the minimum number of bytes to process for socket input operations. The default value for SO_RCVLOWAT is 1. If SO_RCVLOWAT is set to a larger value, blocking receive calls normally wait until they have received the smaller of the low water mark value or the requested amount. (They may return less than the low water mark if an error occurs, a signal is caught, or the type of data next in the receive queue is different from that returned; for example, out-of-band data.) This option takes anint value. Note that not all implementations allow this option to be set.
SO_RCVTIMEO
Sets the timeout value that specifies the maximum amount of time an input function waits until it completes. It accepts a timeval structure with the number of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for this much time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data is received. The default for this option is zero, which indicates that a receive operation shall not time out. This option takes a timeval structure. Note that not all implementations allow this option to be set.
SO_SNDLOWAT
Sets the minimum number of bytes to process for socket output operations. Non-blocking output operations shall process no data if flow control does not allow the smaller of the send low water mark value or the entire request to be processed. This option takes an int value. Note that not all implementations allow this option to be set.
SO_SNDTIMEO
Sets the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. If a send operation has blocked for this time, it shall return with a partial count or with errno set to [EAGAIN] or [EWOULDBLOCK] if no data is sent. The default for this option is zero, which indicates that a send operation shall not time out. This option stores a timeval structure. Note that not all implementations allow this option to be set.
For Boolean options, 0 indicates that the option is disabled and 1 indicates that the option is enabled.

Options at other protocol levels vary in format and name.

Copyright © Linux教程網 All Rights Reserved