您在哪个套接字(clientSocket =accept() 或listen(socket))上设置了sockopt SO_KEEPALIVE?

发布于 2024-09-06 03:31:01 字数 90 浏览 6 评论 0原文

哪个套接字,clientSocket =accept() 或listen(socket),您设置sockopt SO_KEEPALIVE 以使与客户端的连接不被丢弃?

Which socket, the clientSocket = accept() or the listen(socket), do you setsockopt SO_KEEPALIVE on to get the connection to clients not to drop?

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

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

发布评论

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

评论(3

り繁华旳梦境 2024-09-13 03:31:01

在每个接受的套接字上设置选项似乎是最可靠和可移植的。非阻塞模式跨accept的继承在不同实现中是不一致的,并且SO_KEEPALIVE对于监听套接字没有任何意义。

Setting the option on each accepted socket would seem most reliable and portable. Inheritance of non-blocking mode across accept is inconsistent across implementations, and SO_KEEPALIVE has no meaning for the listening socket.

柠檬色的秋千 2024-09-13 03:31:01

SO_KEEPALIVE 只能在连接的 TCP 套接字上工作(因为它需要在计时器上发送数据包),而侦听套接字则不然。

请注意,此选项不会防止连接断开(除了某些损坏的防火墙会在连接不活动一段时间后删除状态)。Stevens 甚至称其为“死亡时通知”。

有关更多详细信息,请参阅此 HOWTO 文档。

SO_KEEPALIVE can only work on connected TCP socket (since it requires a packet being sent on a timer), which listening socket is not.

Note that this option will not keep the connection from dropping (aside from some broken firewalls that remove states after period of connection inactivity.) Stevens even called it "notify when dead" instead.

Take a look at this HOWTO document for more details.

man 页面:

如果 SO_KEEPALIVE 套接字选项为
在已建立的 TCP 上启用
连接并且连接有
空闲了两个小时,TCP 发送一个
数据包发送到远程套接字,
期望远程 TCP
确认连接是
仍然活跃。如果远程 TCP
TCP没有及时响应
继续发送keepalive数据包
根据其正常重传
算法。如果远程 TCP 不
在特定时间内做出响应
限制,TCP 会断开连接。这
下一个套接字系统调用(例如,
recv()) 返回错误,errno 为
设置为 ETIMEDOUT。

有关启用 SO_KEEPALIVE 的详细信息,请参阅 getsockopt(2)。`

在与对等点没有发生任何通信的空闲期后,将向远程对等点发送一个探测(小数据包),以测试对等点是否仍然存在或不是。

如果对端仍然可用,则会收到ACK消息,因此可以推断链路仍然有效。如果没有,则对等方死亡(实际上,情况有点复杂,因为 TCP 在放弃之前会进行一些重试)。

您可以在客户端和服务器上都设置该选项(这意味着服务器和客户端都会收到有关网络问题的通知)。无论如何,在服务器上,您将在 accept() 返回的套接字上设置选项。

From the man page:

If the SO_KEEPALIVE socket option is
enabled on an established TCP
connection and the connection has
been idle for two hours, TCP sends a
packet to the remote socket,
expecting the remote TCP to
acknowledge that the connection is
still active. If the remote TCP does
not respond in a timely manner, TCP
continues to send keepalive packets
according to its normal retransmission
algorithm. If the remote TCP does not
respond within a particular time
limit, TCP drops the connection. The
next socket system call (for example,
recv()) returns an error, and errno is
set to ETIMEDOUT.

See getsockopt(2) for details on enabling SO_KEEPALIVE.`

After an idle period with a peer in which no communication has occurred, a probe (small packet) will be sent to the remote peer in order to test if the peer is still alive or not.

If the peer is still available, an ACK message will be received, so it can be deduced that the link is still on. If not, the peer died (actually it's a little bit more complicated because TCP will do some retries before giving up).

You can set up the option both on the client and the server (this means that both the server and the client will be notified of netowrk problems). In any case, on the server you will set the option on the socket returned by accept().

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