您在哪个套接字(clientSocket =accept() 或listen(socket))上设置了sockopt SO_KEEPALIVE?
哪个套接字,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每个接受的套接字上设置选项似乎是最可靠和可移植的。非阻塞模式跨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.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
页面:在与对等点没有发生任何通信的空闲期后,将向远程对等点发送一个探测(小数据包),以测试对等点是否仍然存在或不是。
如果对端仍然可用,则会收到
ACK
消息,因此可以推断链路仍然有效。如果没有,则对等方死亡(实际上,情况有点复杂,因为TCP
在放弃之前会进行一些重试)。您可以在客户端和服务器上都设置该选项(这意味着服务器和客户端都会收到有关网络问题的通知)。无论如何,在服务器上,您将在
accept()
返回的套接字上设置选项。From the
man
page: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 becauseTCP
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()
.