GPS 发表于 19-9-2010 23:25:13

Socket长连接怎样保持

怎样保持Socket长连接, 在以下环境下:
1.LAN
2.INTERNET
3.wireless
都要用heartbeat吗?heartbeat有多可靠,还要有自己的协议吗?
C++有现成的库吗?

GPS 发表于 20-9-2010 16:45:12

高手指点一下阿。

nilei 发表于 22-9-2010 23:08:55

这是一个很复杂很复杂很复杂的问题。
不开玩笑,很严肃,就以udp为例,要让两个机器能可靠的连接起来,并且长期保持通讯,我们现在的库里实现这个用了大概1万多行。

atelier 发表于 23-9-2010 00:09:14

需要heartbeat,鉴定完毕。

并且看你用什么系统,有的system routine直接可以支持。

GPS 发表于 23-9-2010 17:08:37

限在tcp socket吧,udp就太难讲了。
在LAN里,如果没有代理的话,应该不用heart beat里吧,因为只要连接断开,双方都会有error/exception,对不对?
在internet上,和wireless情况下,我就不太清楚了。我碰到过断开时,server端不一定察觉的,这时需要server端的heart beat. 是否两端都要做heart-beat呢? heartbeat一般怎么做呢?
其实,heartbeat 也保证不了100%,因为可能在相邻heartbeat中读写socket. 是不是还要在应用里做自己的协议呢,比如, send-ack ?
从另一个角度,既然100%很难,是不是就不追求了,只要能够在数据不一致的情况下,能够报错,人工处理?
这些个选择哪里有推荐的阅读吗?

coredump 发表于 23-9-2010 17:55:43

原帖由 GPS 于 23-9-2010 17:08 发表 http://www.freeoz.org/ibbs/images/common/back.gif
在LAN里,如果没有代理的话,应该不用heart beat里吧,因为只要连接断开,双方都会有error/exception,对不对?..
不对


如果没有数据传输,链接会一直显示处于链接状态, 很容易模拟的, 用2个交换机, 每个机器链接一个交换机,你试着断开交换机中间的链接就知道了。这就是为什么很多软件有个 keepalive选项,keepalive就是简单的心跳机制, 比如putty用ssh 连接server时。

所以可靠性要求高的场合,仍然需要心跳机制,有些是在TCP之上自行定义协议,每个请求数据包都对应应答数据包,这些就是协议定义的细节了

[ 本帖最后由 coredump 于 23-9-2010 18:02 编辑 ]

GPS 发表于 23-9-2010 18:43:39

查到可以设置socket的keepalive option。这应该就可以实现心跳了。
有了心跳,还要自行协议吗? 他们是不是同一等级的可靠度?

GPS 发表于 23-9-2010 18:49:50

有个链接讲keepalive

http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/

nilei 发表于 23-9-2010 22:34:50

原帖由 coredump 于 23-9-2010 17:55 发表 http://www.freeoz.org/ibbs/images/common/back.gif

这就是为什么很多软件有个 keepalive选项,keepalive就是简单的心跳机制, 比如putty用ssh 连接server时。

这最主要是防止中间的stateful firewall或者NAT阻断你已经业已建立的使用中的但过去数十秒没有任何通讯的tcp连接。
putty如果是用这个heartbeat去检测是否中间网断了,检测出来又有什么用?难道重新自动连接并提示用户输入密码?:lol

coredump 发表于 23-9-2010 22:42:28

原帖由 GPS 于 23-9-2010 18:43 发表 http://www.freeoz.org/ibbs/images/common/back.gif
查到可以设置socket的keepalive option。这应该就可以实现心跳了。
有了心跳,还要自行协议吗? 他们是不是同一等级的可靠度?
不是一件事, nilei说得比我更准确些。

coredump 发表于 23-9-2010 22:46:30

原帖由 nilei 于 23-9-2010 22:34 发表 http://www.freeoz.org/ibbs/images/common/back.gif


这最主要是防止中间的stateful firewall或者NAT阻断你已经业已建立的使用中的但过去数十秒没有任何通讯的tcp连接。
putty如果是用这个heartbeat去检测是否中间网断了,检测出来又有什么用?难道重新自动连接并提 ... keepalive的确有一些heartbeat的作用,检测出来即使是仅用来作为出错提示也是有意义的。 不同的是tcp keepalive是在tcp协议层的, heartbeat是网络应用层的。 keepalive效率更高,heartbeat功能更强更灵活。

nilei 发表于 23-9-2010 23:00:18

原帖由 coredump 于 23-9-2010 22:46 发表 http://www.freeoz.org/ibbs/images/common/back.gif
keepalive的确有一些heartbeat的作用,检测出来即使是仅用来作为出错提示也是有意义的。 不同的是tcp keepalive是在tcp协议层的, heartbeat是网络应用层的。 keepalive效率更高,heartbeat功能更强更灵活。

keepalive如字面说的,是确保连接不被错误的好心阻断。
heartbeat是用来告知对方"my bloody process is still up and running"

keepalive多用软件在应用层实现,tcp的keep alive是一个dup ack,tcp应该协议没有keepalive这东西,是个取巧。这也就是为什么很多tcp的实现并没有这个“功能”,但同样的,你送一个keepalive过去,对方会回复一个keepalive过来,因为就是一个dup ack而已。在协议层就算没有任何支持,也没有任何取巧,也可以在应用层很高效的做keepalive或者heartbeat。比如我们的软件就是送一个内容为空的udp包,确保中间链路上nat/firewall不关闭udp nat session。

:lol 真正头痛的是算个合理的keepalive间隔,各个设备要求不一,你又无法知道中间到底有什么,送太多太快还会被系统管理员骂,当你在用垃圾软件污染网络。比如至今几个adobe项目也是用20秒这个间隔,而这又恰好是在某大牌厂商设备上不够。本周刚改掉项目里一个这个bug,所以知道多一点。

以后多互相讨论互通有无。

[ 本帖最后由 nilei 于 23-9-2010 23:03 编辑 ]

coredump 发表于 23-9-2010 23:00:42

Keep Alive vs Heartbeat - What's the diff?

http://www.mail-archive.com/mqseries@akh-wien.ac.at/msg04894.html

>What is the difference between these two, if any? Seems to me they both
>accomplish the same thing (allowing one side of a channel to see that the
>network is down and thus being able to go Inactive). The only thing I can
>guess is that Keep Alive applies only to TCP while Heartbeat will work for
>other transmission protocols as well as TCP.

>Was Keep Alive the only thing available at one time, and now is kind of
>useless if you have HeartBeat available to you?

Peter,


There is certainly considerable overlap between the benefits of using
Heartbeat and KeepAlive but they are not mutually exclusive. I would always
recommend that a customer uses both unless he/she has a good reason not to.
A good reason might be that you pay for network packets and you don't want
keepalive flows. Having said that both keepalive packets and heartbeat
packets will only flow when the channels not doing anything so there is no
cost of either if the channel remains active.

Heartbeats are useful for all channel types except SVRCONN channels. A
SVRCONN channel can not 'heartbeat' to the client between MQI calls because
there will be no code at the client ready to respond to the 'heartbeat'. To
detect client applications ending without MQDISC it is therefore imperative
to use KEEPALIVE on your server machine.

Generally speaking Heartbeats are more reliable than KEEPALIVE since they
are proactive. In other words they require that a process on the other end
of the socket actually responds to the request. KEEPALIVE can give false
positives because the sockets may be still alive but the process itself is
hung (say in a channel exit). Heartbeat is also more configurable (ie.
settable on a channel basis), only zOS allows the KEEPALIVE interval to be
set per channel.

Heartbeating is negotiable between the two ends of the channel. So if the
administrator on the other end has configured no heartbeating the value in
your channel definition will be ignored. For this reason I would always use
KEEPALIVE as well as a 'last resort' type thing. Generally speaking
Heartbeats will detect failures because they are more reliable and more
configurable but in some instances we have to fall back and use good old
KEEPALIVE.

Hope this helps,
P.

Paul G Clarke
WebSphere MQ Development
IBM Rochester,MN

GPS 发表于 24-9-2010 22:27:12

够复杂,继续看去。
页: [1]
查看完整版本: Socket长连接怎样保持