关于setsockopt()和getsockopt()函数

发布于 2024-10-03 04:05:20 字数 53 浏览 2 评论 0原文

套接字选项的用途是什么,即套接字编程中的setsockopt()和getsockopt()?

for what especially the socket options are used i.e setsockopt() and getsockopt() in socket programming ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

§对你不离不弃 2024-10-10 04:05:20

例如,您想设置或知道接收缓冲区大小

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) 重用地址

 int on = 1;
 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)

For example you want to set or know receive buffer size

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

 int on = 1;
 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
糖粟与秋泊 2024-10-10 04:05:20

正如已经提到的,它们用于设置/获取套接字的各种选项。

例如,如果您正在测试崩溃的服务器应用程序,您不会在内核允许您重用端口之前等待一定的分钟数,以避免出现“地址已在使用中”错误消息。如果您使用 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).

东走西顾 2024-10-10 04:05:20

对于许多不同的事情,包括更改大小发送和接收缓冲区、超时长度、多播、保持连接活动、禁用 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.

一个人的旅程 2024-10-10 04:05:20

从表面上看,套接字看起来像一个双向管道,这很有用,因为可以在它们上使用诸如 writereadclose 之类的标准系统调用,就像在普通管道甚至文件上。即使您添加特定于套接字的调用(listenconnectbindaccept),也有一个有用的方法隐藏细节以支持流或数据报套接字概念的抽象级别。

但是,一旦特定于协议的细节开始发挥作用并且需要调整特定设置(例如发送/接收缓冲区、超时设置),就需要一个非常通用的接口来考虑不同的设置及其特定的数据格式。 getsockoptsetsockopt 是此通用接口的一部分。

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);

使用 leveloptname 选择特定于协议的选项,并且特定于协议的数据隐藏在缓冲区中,因此这两个系统调用不需要了解有关操作系统可能支持的每个协议的设置——如果您的应用程序和实际的协议实现了解这些细节就足够了。

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.

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 level and optname 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文