关于setsockopt()和getsockopt()函数
套接字选项的用途是什么,即套接字编程中的setsockopt()和getsockopt()?
for what especially the socket options are used i.e setsockopt() and getsockopt() in socket programming ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
例如,您想设置或知道接收缓冲区大小
1)
2) 重用地址
For example you want to set or know receive buffer size
1)
2) Reuse address
正如已经提到的,它们用于设置/获取套接字的各种选项。
例如,如果您正在测试崩溃的服务器应用程序,您不会在内核允许您重用端口之前等待一定的分钟数,以避免出现“地址已在使用中”错误消息。如果您使用 SO_REUSEADDR 选项,让其他套接字绑定到同一端口,除非已经绑定了活动侦听器,则可以避免这种情况。
您还可以在 Linux 计算机上使用
TCP_INFO
检索有关套接字的数据,例如丢失数据包/重传的数量等。基本上,您可以配置所有精细设置。
setsockopt(2) 和 getsockopt(2)。
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_REUSEADDR
option, letting other sockets to bind to the same port unless there is an active listener bound already.You can also retrieve data about a socket, such as the number of lost packets / retransmissions etc by using the
TCP_INFO
on linux machines.Basically, you can configure all the fine settings.
Options for setsockopt(2) and getsockopt(2).
对于许多不同的事情,包括更改大小发送和接收缓冲区、超时长度、多播、保持连接活动、禁用 Nagel 算法等。
根据您要交互的网络层,有不同级别的选项:套接字本身、IP、TCP 等。
For many different things including changing the size of send and receive buffers, length of timeouts, multicasting, keeping the connection alive, disabling Nagel algorithm, etc.
There are levels of options depending on what network layer you what to interact with: socket itself, IP, TCP, and so forth.
从表面上看,套接字看起来像一个双向管道,这很有用,因为可以在它们上使用诸如
write
、read
、close
之类的标准系统调用,就像在普通管道甚至文件上。即使您添加特定于套接字的调用(listen
、connect
、bind
、accept
),也有一个有用的方法隐藏细节以支持流或数据报套接字概念的抽象级别。但是,一旦特定于协议的细节开始发挥作用并且需要调整特定设置(例如发送/接收缓冲区、超时设置),就需要一个非常通用的接口来考虑不同的设置及其特定的数据格式。
getsockopt
、setsockopt
是此通用接口的一部分。使用
level
和optname
选择特定于协议的选项,并且特定于协议的数据隐藏在缓冲区中,因此这两个系统调用不需要了解有关操作系统可能支持的每个协议的设置——如果您的应用程序和实际的协议实现了解这些细节就足够了。Superficially, sockets look like a bidirectional pipe which is useful because standard system calls such as
write
,read
,close
can 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.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
,setsockopt
are part of this generic interface.The protocol-specific options are selected using
level
andoptname
and 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.