如何使用SO_KEEPALIVE选项适当地检测到在另一端的客户端是下来? [英] How to use SO_KEEPALIVE option properly to detect that the client at the other end is down?

查看:118
本文介绍了如何使用SO_KEEPALIVE选项适当地检测到在另一端的客户端是下来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我努力学习选项SO_KEEPALIVE的socket编程的C语言Linux环境下的使用。

so I was trying to learn the usage of option SO_KEEPALIVE in socket programming in C language under Linux environment.

我创建了一个服务器套接字,并用我的浏览器连接到它。这是成功的,我能够读取GET请求,但我被困在SO_KEEPALIVE的用法。

I created a server socket and used my browser to connect to it. It was successful and I was able to read the GET request, but I got stuck on the usage of SO_KEEPALIVE.

我检查这个链接 keepalive_description@tldg.org ,但我找不到它的任何节目例如如何使用它。

I checked this link keepalive_description@tldg.org but I could not find any example which shows how to use it.

当我发现客户对)的请求接受(功能我设置了 SO_KEEPALIVE 选项值 1 客户端套接字。
现在,我不知道如何检查,如果客户端是向下?如何改变发送等探针之间的时间间隔。

As soon as I detect the client's request on accept() function I set the SO_KEEPALIVE option value 1 on the client socket. Now I dunno how to check if the client is down?, How to change the time interval between the probes sent etc.

我的意思是,我将如何得到信号在客户端已关闭(无阅读或在客户端写...我想我会在探头没有答复从客户回来一些信号),我应该怎么后编程设置该选项SO_KEEPALIVE。

I mean how will I get the signal that the client is down (without reading or writing at the client...I thought I will get some signal when probes are not replied back from client), how should I program it after setting the option SO_KEEPALIVE on.

此外,如果假定探头发出的每3秒和客户端下降之间我不会知道客户端下来,我可能会得到SIGPIPE。

Also if suppose the probes are sent every 3 secs and the client goes down in between I will not get to know that client is down and I may get SIGPIPE.

反正重要的是我想知道如何在code使用SO_KEEPALIVE。

Anyways importantly I wanna know how to use SO_KEEPALIVE in the code.

在此先感谢一吨!

推荐答案

要修改探测器探头间隔的数量,或者将值写到/ proc文件系统像

To modify the number of probes or the probe intervals, you write values to the /proc filesystem like

 echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time
 echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl
 echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes

请注意,这些值是全球系统上所有的存活启用插座,您也可以在当您设置的setsockopt覆盖每个插槽的基础上这些设置,看你链接的文档的第4.2节。

Note that these values are global for all keepalive enabled sockets on the system, You can also override these settings on a per socket basis when you set the setsockopt, see section 4.2 of the document you linked.

您不能从用户空间的检查插座的状态与保活。相反,内核仅仅是更积极的有关强制远程端确认的数据包,并确定如果插座坏了。当您尝试写入套接字,你会得到一个SIGPIPE如果保活已经确定远端关闭。

You can't "check" the status of the socket from userspace with keepalive. Instead, the kernel is simply more aggressive about forcing the remote end to acknowledge packets, and determining if the socket has gone bad. When you attempt to write to the socket, you will get a SIGPIPE if keepalive has determined remote end is down.

这篇关于如何使用SO_KEEPALIVE选项适当地检测到在另一端的客户端是下来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆