【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).
當操作套接字選項時,必須指定 選項的級別 及 選項的名稱。
在套接字接口層操作選項時,指定 level 為SOL_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.
參數 optval 和 optlen 用于獲取 setsockopt() 的選項值。
對于函數getsockopt(),它們(optval 和 optlen)表示一個緩沖區,請求選項的內容將在其中返回。
對于函數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(錯誤)
| 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 是未知的。 |
| ENOTSOCK | The 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/.
注:上述翻譯是機翻加人工修正,可能還有不理解的地方導致翻譯得不是很恰當;
總結
以上是生活随笔為你收集整理的【man】getsockopt 和 setsockopt的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机科学与技术网上书店,计算机科学与技
- 下一篇: 计算机系统要素--第六章,02-HACK