C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)
生活随笔
收集整理的這篇文章主要介紹了
C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 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函數
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的C语言socket getsockopt() setsockopt()函数(获取和设置套接口的选项?)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: epoll(eventpoll)是干嘛的
- 下一篇: C语言socket bind()函数(为