C语言 关于setsockopt() 和getsockopt() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4233598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
about setsockopt() and getsockopt() function
提问by user507401
for what especially the socket options are used i.e setsockopt() and getsockopt() in socket programming ?
在套接字编程中使用套接字选项即setsockopt() 和getsockopt() 是为了什么?
回答by Sanja Melnichuk
For example you want to set or know receive buffer size
例如你想设置或知道接收缓冲区大小
1)
1)
int skt, int sndsize;
err = setsockopt(skt, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize,
(int)sizeof(sndsize));
err = getsockopt(skt, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize, &size);
2) Reuse address
2) 复用地址
int on = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
回答by Duck
For manydifferent things including changing the size of send and receive buffers, length of timeouts, multicasting, keeping the connection alive, disabling Nagel algorithm, etc.
对于许多不同的事情,包括更改发送和接收缓冲区的大小、超时长度、多播、保持连接活动、禁用 Nagel 算法等。
There are levels of options depending on what network layer you what to interact with: socket itself, IP, TCP, and so forth.
根据您要与之交互的网络层,有多种选项:套接字本身、IP、TCP 等等。
回答by Milan
As already mentioned they are used for setting/getting various options for a socket.
如前所述,它们用于设置/获取套接字的各种选项。
For example, if you are testing a server application that crashes, you don't wont to wait a certain number of minutes before the kernel let you reuse the port avoiding the "Address already in use" error messages. This can be avoided if you use the SO_REUSEADDRoption, letting other sockets to bind to the same port unless there is an active listener bound already.
例如,如果您正在测试崩溃的服务器应用程序,您不会在内核允许您重用端口之前等待特定分钟数以避免“地址已在使用中”错误消息。如果您使用该SO_REUSEADDR选项,则可以避免这种情况,除非已经绑定了活动侦听器,否则让其他套接字绑定到同一端口。
You can also retrieve data about a socket, such as the number of lost packets / retransmissions etc by using the TCP_INFOon linux machines.
您还可以使用TCP_INFOlinux 机器检索有关套接字的数据,例如丢失的数据包/重新传输的数量等。
Basically, you can configure all the fine settings.
基本上,您可以配置所有精细设置。
Options for setsockopt(2)and getsockopt(2).
对于选项setsockopt的(2)和的getsockopt(2) 。
回答by hillu
Superficially, sockets look like a bidirectional pipe which is useful because standard system calls such as write, read, closecan be used on them just like on normal pipes or even files. Even if you add socket-specific calls (listen, connect, bind, accept), there is a useful level of abstraction that hides away details in favor of the notion of streaming or datagram sockets.
从表面上看,套接字看起来像一个双向管道,这很有用,因为可以像在普通管道甚至文件上一样在它们上使用标准系统调用,例如write, read, close。即使您添加特定于套接字的调用(listen, connect, bind, accept),也有一个有用的抽象级别可以隐藏细节以支持流或数据报套接字的概念。
But as soon as protocol-specific details come into play and specific settings need to be tuned (for example send/receive buffers, timeout settings), a very generic interface is needed to account for the different settings and their specific data formats. getsockopt, setsockoptare part of this generic interface.
但是,一旦特定协议的细节发挥作用并且需要调整特定设置(例如发送/接收缓冲区、超时设置),就需要一个非常通用的接口来说明不同的设置及其特定的数据格式。getsockopt,setsockopt是这个通用接口的一部分。
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);
The protocol-specific options are selected using leveland optnameand the protocol-specific data is hidden in a buffer, so the two system calls do not need to know anything about the settings of every protocol the OS may support -- it's enough if your application and the actual protocol implementation know about those details.
使用level和选择协议特定的选项,optname并且协议特定的数据隐藏在缓冲区中,因此这两个系统调用不需要了解操作系统可能支持的每个协议的设置——如果您的应用程序和实际的协议实现知道这些细节。

