设为首页 收藏本站
查看: 1467|回复: 0

[经验分享] What is the best way to implement a heartbeat in C++ to check for socket connect

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-21 10:11:21 | 显示全部楼层 |阅读模式
  Hey gang.  I have just written a client and server inC++ using sys/socket.  I need to handle a situation where the client isstill active but the server is down.  One suggested way to do this is to use a heartbeat to periodically assert connectivity.  And if there isnone to try to reconnect every X seconds for Y period of time, and thento time out.
  Is this "heartbeat" the best way to check for connectivity?
  The socket I am using might have information on it, is there a way tocheck that there is a connection without messing with the buffer?
  
  
down voteaccepted  If you're using TCP sockets over an IP network, you can use the TCP protocol's keepalive feature, which will periodically check the socket to make sure the other end is still there. (This also has the advantage of keeping the forwarding record foryour socket valid in any NAT routers between your client and your server.)
  Here's a TCP keepalive overviewwhich outlines some of the reasons you might want to use TCP keepalive; this Linux-specific HOWTOdescribes how to configure your socket to use TCP keepalive at runtime.
  It looks like you can enable TCP keepalive in Windows sockets by setting SIO_KEEPALIVE_VALSusing the WSAIoctl()function.
  If you're using UDP sockets over IP you'll need to build your own heartbeat into your protocol.
link|flag

  If the other side has gone away (i.e. the process has died, the machine has gone down, etc.), attemptingto receive data from the socket should result in an error. However if the other side is merely hung, the socket will remain open. In this case, having a heartbeat is useful. Make sure that whatever protocol youare using (on top of TCP) supports some kind of "do-nothing" request orpacket - each side can use this to keep track of the last time they received something from the other side, and can then close the connection if too much time elapses between packets.
  Note that this is assuming you're using TCP/IP. If you're using UDP, then that's a whole other kettle of fish, since it's connectionless.


  Ok, I don't know what your program does or anything, so maybe this isn't feasible, but I suggest that you avoid trying to always keep the socket open. It should only be open when you are using it, and should be closed when you are not.
  If you are between reads and writes waiting on user input, close the socket. Design your client/server protocol (assuming you're doing this by hand and not using any standard protocols like http and/or SOAP) to handle this.
  Sockets will error if the connection is dropped; write your program such that you don't lose any information in the case of such an error during a write to the socket and that you don't gain any information in the case of an error during a read from the socket. Transactionality andatomicity should be rolled into your client/server protocol (again, assuming you're designing it yourself).
  
What's the advantage of closing and re-opening the socket all the time? – Graeme PerrowJan 30 '09 at 15:56You aren't dependent on or wastingexternal resources. Again, I don't know what the program does, but mostprograms spend more time waiting for user input than they do writing orreading to/from a network. There's no need for the socket to be open atthose times.  – RandolphoJan 30 '09 at 16:28Meh. You're adding extra code and extra work to save yourself one socket. Web browsers usedto work this way but it was too slow to keep dropping and re-creating connections. That's why keepalive was added in HTTP/1.1. – Graeme PerrowJan 30 '09 at 16:45And does any modern browser keep the socket open between user requests? No, it does not. Keepalive was added because getting everything involved in a single web page really constitutes a single request and it's therefore better to keep using thesame socket. – RandolphoJan 30 '09 at 17:18Also, I think we disagree as to what "extra code and extra work" means. I personally think a program that tries to keep a socket open all the time no matter what is going pretty deep into the realm of "extra code and extra work". – RandolphoJan 30 '09 at 17:19show 4more comments

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-141741-1-1.html 上篇帖子: 在 CentOS 中用 Heartbeat/DRBD 建立高可用性群集 下篇帖子: hadoop中的Heartbeat
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表