C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)(套接字级别SOL_SOCKET)
参考文章:linux 中socket编程中setsockopt()函数功能介绍
·
文章目录
man 文档(越看越懵逼啊!)
GETSOCKOPT(2) Linux Programmer's Manual GETSOCKOPT(2)
NAME
getsockopt, setsockopt - get and set options on sockets //获取并设置套接字上的选项
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()操作文件描述符sockfd引用的套接字的选项。
//选项可能存在于多个协议级别;
//它们总是出现在最上面的socket级别。
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;
//在操作套接字选项时,必须指定选项所在的级别和选项的名称。
//要在sockets API级别操作选项,级别被指定为SOL_SOCKET。
//要在任何其他级别操作选项,需要提供控制该选项的相应协议的协议号。
//例如,为了指示某个选项将由TCP协议解释,应将级别设置为TCP的协议号;
see 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.
//参数optval和optlen用于访问setsockopt()的选项值。
//对于getsockopt(),它们标识一个缓冲区,在该缓冲区中返回所请求选项的值。
//对于getsockopt(),optlen是一个值结果参数,最初包含optval指向的缓冲区大小,并在返回时进行修改,以指示返回值的实际大小。
//如果不提供或返回选项值,optval可能为空。
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 sec‐
tion 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.
//大多数套接字级别的选项都为optval使用int参数。
//对于setsockopt(),参数应为非零以启用布尔选项,如果要禁用该选项,则参数应为零。
For a description of the available socket options see socket(7) and the appropriate protocol man pages.
//有关可用套接字选项的说明,请参阅套接字(7)和相应的协议手册页。
RETURN VALUE
On success, zero is returned for the standard options. On error, -1 is returned, and errno is set appropriately.
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.
ERRORS
EBADF The argument sockfd is not a valid descriptor.
EFAULT 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.
EINVAL 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)).
ENOPROTOOPT
The option is unknown at the level indicated.
ENOTSOCK The file descriptor sockfd does not refer to a socket.
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.
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).
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
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/.
Linux 2015-12-28 GETSOCKOPT(2)
解释
通常用getsockopt和setsockopt两个函数来获取和设置套接口的选项
getsockopt()函数用于获取任意类型、任意状态套接口的选项当前值,并把结果存入optval。
#include <sys/socket.h>
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
/*
sockfd:一个标识套接口的描述字。
level:选项定义的级别。例如,支持的级别有SOL_SOCKET、IPPROTO_TCP等。
optname:需获取的套接口选项。
optval:指针,指向存放所获得选项值的缓冲区。
optlen:指针,指向optval缓冲区的长度值。
*/
setsockopt()函数用于任意类型、任意状态套接口的设置选项值。尽管在不同协议层上存在选项,但本函数仅定义了最高的“套接口”
(?)级别上的选项。
#include <sys/socket.h>
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
/*
sockfd:标识一个套接口的描述字。
level:选项定义的级别;支持SOL_SOCKET、IPPROTO_TCP、IPPROTO_IP和IPPROTO_IPV6等。
optname:需设置的选项。
optval:指针,指向存放选项值的缓冲区。
optlen:optval缓冲区长度。
*/
总结
以上两个函数仅用于套接口,sockfd必须指向一个打开的套接口, level指定系统中解释选项的代码,普通套接口代码或特定于协议的代码(例如:IPv4、IPv6或TCP)。
optval是一个指向变量的指针,通过它,或由setsockopt取得选项的新值,或由getsockopt存储选项的当前值。此变量的大小由最后一个参数指定,对于setsockopt,它是一个值,对getsockopt来说,它是一个出参。
下表总结了由getsockopt获取或由setsockopt设置的一些选项,“数据类型”列给出了指针optval必须指向的每个选项的数据类型。其中,用花括号的标记来表示一个结构,如linger{}表示结构linger。
参考文章1:linux 中socket编程中setsockopt()函数功能介绍
20220512 setsockopt()各使用场景
参考文章:C语言socket服务端报错:bind socket error: Address already in use(errno: 98)(setsockopt()各使用场景)
更多推荐
已为社区贡献20条内容
所有评论(0)