【man】getsockopt 和 setsockopt

NAME (名称)

getsockopt, setsockopt - get and set options on sockets

getsockopt, setsockopt - 获取和设置套接字的选项

SYNOPSIS(概要)

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);

DESCRIPTION(描述)

getsockopt() and setsockopt() manipulate options for the socket referred to by the file descriptor sockfd. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.

getsockopt()和setsockopt() 根据设置的选项(option) 操作 文件描述符sockfd。
操作可能处于协议层;它们总是出现在最上面的 套接字层(socket level)。

When manipulating socket options, the level at which the option resides and the name of the option must be specified. To manipulate options at the sockets API level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP; see getprotoent(3).

当操作套接字选项时,必须指定 选项的级别选项的名称
在套接字接口层操作选项时,指定 levelSOL_SOCKET,这里提供了 适当的协议控制选项 的 协议号 去 操作操作其它任意层的选项;
举个栗子,如何表明一个选项 能够被理解成TCP协议,level应该被设置为TCP协议号;参考getprotoent(3)。

The arguments optval and optlen are used to access option values for setsockopt(). For getsockopt() they identify a buffer in which the value for the requested option(s) are to be returned. For getsockopt(), optlen is a value-result argument, initially containing the size of the buffer pointed to by optval, and modified on return to indicate the actual size of the value returned. If no option value is to be supplied or returned, optval may be NULL.

参数 optvaloptlen 用于获取 setsockopt() 的选项值。
对于函数getsockopt(),它们(optvaloptlen)表示一个缓冲区,请求选项的内容将在其中返回。
对于函数getsockopt(),optlen 是一个传入传出参数,传入时指的是optval 指向的缓冲区的初始化大小,并在返回时修改以指示返回值的实际大小。
如果没有提供 或 返回 选项值,那么 optval 可能为 NULL。

Optname and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. The include file <sys/socket.h> contains definitions for socket level options, described below. Options at other protocol levels vary in format and name; consult the appropriate entries in section 4 of the manual.

Optname 和任何指定的选项 未经解释地 传递给 对应协议模块 进行解释。
头文件 <sys/socket.h> 包含 套接字级别选项 的定义,如下所述。
其他协议级别的选项 格式和名称上有所不同; 请查阅手册第 4 节中的相应条目

Most socket-level options utilize an int argument for optval. For setsockopt(), the argument should be nonzero to enable a boolean option, or zero if the option is to be disabled.

大多数 使用 int 参数 来表示 套接字选项optval 的 等级。
对于函数setsockopt(),参数(指的应该是level)应该为非0来启用布尔选项,或者通过数值0来禁用该选项。

For a description of the available socket options see socket(7) and the appropriate protocol man pages.

有关可用套接字选项的描述,请参见 socket(7) 和相应的协议手册页。

RETURN VALUE(返回值)

On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.

成功时返回0。失败时返回-1,并设置错误码。

Netfilter allows the programmer to define custom socket options with associated handlers; for such options, the return value on success is the value returned by the handler.

Netfilter 允许程序员使用相关的 处理程序(handler) 定义自定义套接字选项;
对于此类选项,成功时的返回值是 处理程序(handler) 返回的值。

ERRORS(错误)

ERRORSEnglish DESCRIPTION中文说明
EBADF
(Error: Bad Fd)
The argument sockfd is not a valid descriptor.无效的参数sockfd
EFAULT
(Error: Fault)
The address pointed to by optval is not in a valid part of the process address space. For getsockopt(), this error may also be returned if optlen is not in a valid part of the process address space.optval 指向的地址不在进程地址空间的有效部分。 对于函数 getsockopt(),如果指针参数 optlen 不在进程地址空间的有效部分中,也可能返回此错误。
EINVAL
(Error: invalid)
optlen invalid in setsockopt(). In some cases this error can also occur for an invalid value in optval (e.g., for the IP_ADD_MEMBERSHIP option described in ip(7)).在 setsockopt() 中 optlen 无效。 在某些情况下,optval 中的无效值也会发生此错误(例如,对于 ip(7) 中描述的 IP_ADD_MEMBERSHIP 选项)。
ENOPROTOOPT
(Error: No Protocol Option)
The option is unknown at the level indicated.在指定的 level 上,该 option 是未知的。
ENOTSOCKThe file descriptor sockfd does not refer to a socket.输入的文件描述符sockfd并不是个套接字

CONFORMING TO(符合…)

POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).

NOTES(说明)

POSIX.1 does not require the inclusion of <sys/types.h>, and this header file is not required on Linux. However, some historical (BSD) implementations required this header file, and portable applications are probably wise to include it.

POSIX.1 不需要包含 <sys/types.h>,Linux 上也不需要这个头文件。
但是,一些历史 (BSD) 实现需要此头文件,并且可移植应用程序可能 明智地 包含它。

The optlen argument of getsockopt() and setsockopt() is in reality an int [*] (and this is what 4.x BSD and libc4 and libc5 have). Some POSIX confusion resulted in the present socklen_t, also used by glibc. See also accept(2).

getsockopt() 和 setsockopt() 的 optlen 参数实际上是一个 int [*] (这就是 4.x BSD 和 libc4 和 libc5 所具有的)。
一些 POSIX 混淆? 导致了现在的 socklen_t,glibc 也使用了它。 另请参阅接受 (2)。

BUGS(缺陷)

Several of the socket options should be handled at lower levels of the system.

应该在系统的较低级别处理几个套接字选项。

SEE ALSO(另见)

ioctl(2), socket(2), getprotoent(3), protocols(5), ip(7), packet(7), socket(7), tcp(7), udp(7), unix(7)

COLOPHON - Linux 2015-12-28

This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man-pages/.

注:上述翻译是机翻加人工修正,可能还有不理解的地方导致翻译得不是很恰当;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值