文章目录

  • man 文档(越看越懵逼啊!)
  • 解释
    • getsockopt()函数用于获取任意类型、任意状态套接口的选项当前值,并把结果存入optval。
    • setsockopt()函数用于任意类型、任意状态套接口的设置选项值。尽管在不同协议层上存在选项,但本函数仅定义了`最高的“套接口”`(?)级别上的选项。
    • 总结
  • 20220512 setsockopt()各使用场景

man 文档(越看越懵逼啊!)

GETSOCKOPT(2)                                                        Linux Programmer's Manual                                                       GETSOCKOPT(2)NAMEgetsockopt, 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);DESCRIPTIONgetsockopt()  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 socketsAPI  level,  level  is  specified  as SOL_SOCKET.  To manipulate options at any other level the protocol number of the appropriate protocol controlling theoption 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 therequested option(s) are to be returned.  For getsockopt(), optlen is a value-result argument, initially containing the size of the  buffer  pointed  to  byoptval, 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> containsdefinitions 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 theoption 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 VALUEOn 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  valuereturned by the handler.ERRORSEBADF     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 ifoptlen 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  optiondescribed in ip(7)).ENOPROTOOPTThe option is unknown at the level indicated.ENOTSOCK  The file descriptor sockfd does not refer to a socket.CONFORMING TOPOSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).NOTESPOSIX.1  does  not  require  the inclusion of <sys/types.h>, and this header file is not required on Linux.  However, some historical (BSD) implementationsrequired 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  confusionresulted in the present socklen_t, also used by glibc.  See also accept(2).BUGSSeveral of the socket options should be handled at lower levels of the system.SEE ALSOioctl(2), socket(2), getprotoent(3), protocols(5), ip(7), packet(7), socket(7), tcp(7), udp(7), unix(7)COLOPHONThis  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 versionof 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()函数功能介绍

参考文章2:getsockopt和setsockopt函数

20220512 setsockopt()各使用场景

参考文章:C语言socket服务端报错:bind socket error: Address already in use(errno: 98)(setsockopt()各使用场景)

C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)(套接字级别SOL_SOCKET)相关推荐

  1. C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)

    文章目录 man 文档(越看越懵逼啊!) 解释 getsockopt()函数用于获取任意类型.任意状态套接口的选项当前值,并把结果存入optval. setsockopt()函数用于任意类型.任意状态 ...

  2. R语言names函数获取或者设置数据对象名称实战

    R语言names函数获取或者设置数据对象名称实战 目录 R语言names函数获取或者设置数据对象名称实战 #基本语法

  3. R语言libPaths函数获取或者设置包安装的路径实战

    R语言libPaths函数获取或者设置包安装的路径实战 目录 R语言libPaths函数获取或者设置包安装的路径实战 #.libP

  4. c语言读取文件字节数,怎么在C语言中利用fstat函数获取文件的大小

    怎么在C语言中利用fstat函数获取文件的大小 发布时间:2021-01-22 17:03:17 来源:亿速云 阅读:110 作者:Leah 怎么在C语言中利用fstat函数获取文件的大小?针对这个问 ...

  5. Linux下getsockopt/setsockopt 函数说明

    [getsockopt/setsockopt系统调用]       功能描述:         获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套 ...

  6. TCP/IP编程之getsockopt/setsockopt函数详解

    前述: 有很多方法来获取和设置影响套接字的选项: · getsockopt和setsockopt函数 · fcntl函数,是把套接字设置为非阻塞式I/O型或者信号驱动式I/O型以及设置套接字属主的PO ...

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

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

  8. getsockopt/setsockopt函数用法【转】

    来自:https://blog.csdn.net/legendox/article/details/4595492 功能描述:         获取或者设置 与某个套接字关联的选 项.选项可能存在于多 ...

  9. 使用 fcntl 函数 获取,设置文件的状态标志

    前言 当打开一个文件的时候,我们需要指定打开文件的模式( 只读,只写等 ).那么在程序中如何获取,修改这个文件的状态标志呢?本文将告诉你如何用 fcntl函数 获取指定文件的状态标志. 解决思路 1. ...

最新文章

  1. python yaml用法详解
  2. [C++] Lvalue and Rvalue Reference
  3. c语言创建字符树,使用C语言构建基本的二叉树数据结构
  4. java如何模拟请求_单元测试如何模拟用户请求
  5. UNIX(进程间通信):02---父子进程之间的数据共享分析
  6. SQL Server 2005 中的客户端 XML 处理
  7. php向mysql提交数据_PHP 如何向 MySQL 发送数据
  8. 【路径规划】基于matlab蚁群优化遗传算法求解机器人栅格地图最短路径规划问题【含Matlab源码 1581期】
  9. 数模美赛准备——我的第一个LaTex文档
  10. Windows虚拟机忘记操作系统密码
  11. 【Adobe Premiere Pro 2020】pr模板下载和pr使用模板创建视频、pr调色说明、pr全景视频编辑说明、pr无缝转场特效制作流程、pr保存预设效果和pr使用预设效果
  12. 一次简单的软件系统培训计划安排
  13. 全球及中国报刊行业运营策略与未来发展态势研究报告2022版
  14. linux开机画面视频,Linux系统的开机画面
  15. JAVA并发编程:悲观锁与乐观锁
  16. 在ubuntu上解包RK3288固件
  17. 乐高魔方机器人编程及图纸_魔方机器人教程图纸程序下载【Reinhard Grafl】作品...
  18. Suzy找到实习了吗 Day 1 | 704. 二分查找、27. 移除元素
  19. 3月18日云栖精选夜读 | 开发者必看!探秘阿里云Hi购季开发者分会场:海量学习资源0元起!...
  20. 43款设计师必备英文设计字体-书法字体

热门文章

  1. jq实现移动端横向导航条,点击滚动居中加动画
  2. 校园信息化的发展趋势
  3. Word365英文版,从当前页开始插入页码
  4. Python中内存和变量管理
  5. 23. RAID磁盘阵列——RAID0,RAID1,RAID5,RAID6,RAID10,RAID01,软RAID,硬RAID
  6. 【无标题】高薪的IT行业,你选择学习什么
  7. 黑龙江省齐齐哈尔市谷歌高清卫星地图下载
  8. linux主内网dns辅助外网dns,linux 主DNS和辅助DNS的配置
  9. 盖国强:数据驱动传统行业与互联网融合
  10. Rails Rake指南