linux so_linger,linux系统编程之getsockopt/setsockopt 函数

最近看别人写的代码很多函数不知道啊,在研究分布式消息队列beanstalkd,遇到了很多东西。尤其是网络连接方面。

代码是这样的。

setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof flags);

setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof flags);

setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof linger);

setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof flags);

通过函数名,我们可以晓得,setsockopt 就是设这sock的一些选项,那这些参数都是什么意义呢?

下面就是在网上收集的一些这两个函数的东西。

通过英文文档,我们可以知道

NAME

setsockopt - set the socket options

SYNOPSIS

#include int setsockopt(intsocket, intlevel, intoption_name,

const void *option_value, socklen_toption_len);

功能描述

功能描述:

获取或者设置与某个套接字关联的选 项。选项可能存在于多层协议中,它们总会出现在最上面的套接字层。当操作套接字选项时,选项位于的层和选项的名称必须给出。为了操作套接字层的选项,应该 将层的值指定为SOL_SOCKET。为了操作其它层的选项,控制选项的合适协议号必须给出。例如,为了表示一个选项由TCP协议解析,层应该设定为协议 号TCP。

参数:

sock:将要被设置或者获取选项的套接字。

level:选项所在的协议层。

optname:需要访问的选项名。

optval:对于getsockopt(),指向返回选项值的缓冲。对于setsockopt(),指向包含新选项值的缓冲。

optlen:对于getsockopt(),作为入口参数时,选项值的最大长度。作为出口参数时,选项值的实际长度。对于setsockopt(),现选项的长度。

下面有很多功能选项。

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

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

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 an

int

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.

以上英文的比较难看,给大家一个链接http://blog.csdn.net/beiguoyouzi/article/details/4200761

详细讲述了选项的使用。

RETURN VALUE

成功的话就会返回0,失败返回-1,失败的时候会这是errorno值。下面是几个选项值。

ERRORS

Thesetsockopt() function shall fail if:

[EBADF]

The

socket

argument is not a valid file descriptor.

[EDOM]

The send and receive timeout values are too big to fit into the timeout fields in the socket structure.

[EINVAL]

The specified option is invalid at the specified socket level or the socket has been shut down.

[EISCONN]

The socket is already connected, and a specified option cannot be set while the socket is connected.

[ENOPROTOOPT]

The option is not supported by the protocol.

[ENOTSOCK]

The

socket

argument does not refer to a socket.

Thesetsockopt() function may fail if:

[ENOMEM]

There was insufficient memory available for the operation to complete.

[ENOBUFS]

Insufficient resources are available in the system to complete the call.

更多文章,欢迎访问:http://blog.csdn.net/wallwind

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值