文章目录

  • man 文档(越看越懵逼啊!)
  • 解释
    • getsockopt()函数用于获取任意类型、任意状态套接口的选项当前值,并把结果存入optval。
    • 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函数

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

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

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

  2. setsockopt设置套接口选项

    1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket: BOOL bReuseaddr=TRUE; setsockopt(s,SOL_SOCKET , ...

  3. Linux下getsockopt/setsockopt 函数说明

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

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. Spring-boot+Vue = Fame 写blog的一次小结
  2. python 得到文件行数
  3. SAP的Cloud Platform增加了多云支持和商业语义
  4. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
  5. Android复习09【内容提供者、音乐播放器(附完整工程文件)】
  6. UVA 1156 - Pixel Shuffle(模拟+置换)
  7. 手机版数据库oracle,用手机管理及维护MySQL,Oracle等数据库
  8. 32位和64位机器上C语言数据类型的大小
  9. H.264官方软件JM源代码简单分析-解码器ldecod
  10. 程序员春节回家相亲指南
  11. 培训变成了闹剧,却不想怎么改进
  12. 传统词向量nlp处理的优缺点_吴恩达深度学习笔记(126) | NLP | GloVe 词向量
  13. ubuntu查看显卡驱动以及其他驱动
  14. Wordpress 优化小结 杜绝博客因为CPU 占用过高被封
  15. 微信令人头疼的文件自动下载功能终于不用默认了
  16. 引入YouTube视频自动控制开始和暂停
  17. Python之Selenium模拟浏览器
  18. 腹有诗书气自华——记环宇通软CEO骆永华
  19. Mediakit报告设备商的空间不足以执行此操作的纯MAC解法
  20. 如何有效利用大数据预测能力

热门文章

  1. Asp.net Web.Config - 配置元素 httpCookies
  2. linux终端传文件,如何使用Linux FTP命令传输文件
  3. 关于ERP、MES、SFC问题
  4. VF02 会计凭证过账时间
  5. Oracle取最大值问题
  6. 关于SAP中物料双单位的解析
  7. SAP SHD0的详细简介
  8. “百度智能云”下,群星璀璨,照亮百度世界2020
  9. 计算机二级C语言辅导j机构,全国计算机二级C语言公基础知识辅导.pptx
  10. java用递归删除文件夹_Java中通过递归调用删除文件夹下所有文件