异步套接字和“静默”断开 [英] Asyncsockets and "silent" disconnections

查看:208
本文介绍了异步套接字和“静默”断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asyncsocket将cocoaasyncsocket用作Windows .net服务器的客户端。我使用ProtocolBuffers编码消息。

I've been using cocoaasyncsocket as a client to a Windows .net server using asyncsocket. I am encoding messages using ProtocolBuffers. Together, these make a great set of tools.

但是最近我注意到,如果我离开客户端连接到服务器很长时间 - 很多小时 - 当我尝试请求数据,该消息似乎被发送,但从未到达服务器。我将此称为静音断开连接,因为我没有收到通常的断开连接,如果有网络问题。

However recently I have noticed that if I leave the client connected to the server for a long time - many hours - when I try to make a request for data, the message appears to get sent but never arrives at the server. I'm calling this a "silent" disconnection because I'm not receiving the usual disconnection that I would if there were a network problem.

我正在处理以下方法在我调试,但没有一个被调用:

I am handling the following methods in my effort to debug, but none of them get called:

- (NSTimeInterval)onSocket:(AsyncSocket *)sock
  shouldTimeoutReadWithTag:(long)tag
                   elapsed:(NSTimeInterval)elapsed
                 bytesDone:(CFIndex)length {

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err

- (void)onSocketDidDisconnect:(AsyncSocket *)sock

没有任何通知,我发现这个调试的棘手。

Without any notification, I'm finding this tricky to debug. The server similarly does not show any disconnections.

任何人都能指出如何进一步分析这个问题?

Would anyone be able to point me in a direction to how to further analyze this?

非常感谢。

推荐答案

如果您想知道您的客户端和服务器已连接,心跳或keepalive,即基本上说你在那里/是交换的应用级消息。

If you want to know that your clients and servers are connected, you must have a heartbeat or "keepalive", i.e. an application level message which is exchanged basically saying "Are you there"/"yes".

现在,将静默删除空闲TCP连接。这不是TCP的目的,但它是一个事实。

This is particularly true nowadays as many routers will silently drop idle TCP connections. That's not what TCP was designed for, but it is a fact of life.

TCP有一个选项发送连接层heartbeat或keepalive与SO_KEEPALIVE,但历史上关于这是否适当的争论。设计师认为,由于临时中间网络问题而丢弃套接字是错误的。如果在此期间没有实际发送数据,则没有理由删除连接。其他人认为知道连接是否好是重要的和本身。如果你期待数据,但它没有到达怎么办? Woudl't你想知道吗?

TCP has an option to send connection-layer heartbeat or keepalive with SO_KEEPALIVE, but historically there was an argument over whether this was appropriate. The designers felt that dropping a socket because of a temporary intermediate network problem was wrong. If no data was actually sent during that period, there was no reason to drop the connection. Others felt that knowing whether the connection was good was important in and of itself. What if you were expecting data but it hadn't arrived? Woudln't you want to know?

最终取决于应用程序。如果无新信息是对您的应用程序很重要的事实断言(例如新闻订阅,销售订单,市场数据价格变化),您需要检查无新信息是否为真实的无新信息不是连接静静丢弃。这意味着一个明确的消息。

Ultimately it depends on the application. If "no new information" is an assertion of fact which is important to your application, (for example news feeds, sales orders, market data price changes) you need to check that the "no new information" is a real "no new information" not a "connection silently dropped". That means an explicit message.

那么,你应该多久发送一次?

So how often should you send it?

东西。 1a)您通常获得更新的频率? 1b)什么延迟是可接受的/正常的(例如,如果这是一个过程的一部分,其他步骤通常需要几个小时,然后5-10分钟可能是可接受的)。 2a)电池和2b)由于心跳的数据使用。我怀疑对电池/电量的影响将是至关重要的,但所有这些都需要仔细,平衡。

That depends on a balance of things. 1a) how often do you normally get updates? 1b) What latency is acceptable/normal (e.g. if this is part of a process, other steps take typically hours, then 5-10 minutes might be acceptable). 2a) battery and 2b) data usage due to heartbeat. I suspect the effect on battery/power will be crucial, but all these need to be looked at carefully, and balanced.

您可能随时失去覆盖隧道等)。只有在平均更新间隔的0.5到5倍之间没有更新的情况下,我才会运行心跳。因此,如果你希望每分钟更新2次,运行心跳如果空闲15秒到3分钟 - 判断什么是最好的。如果用户在应用程序实时和关闭完成后,电池寿命不是这样的问题,因为他们无论如何使用它。如果您正在唤醒设备来处理更新,电池寿命确实是一个问题。

You could lose coverage at any time after all (tunnels etc). I would run the heartbeat only if you have had no updates after something between 0.5 and 5 times the average update interval. So if you expect 2 updates per minute, run heartbeats if idle for 15 seconds to 3 minutes - judge what is best. Provided the user is in the app "live" and "turns it off" when done, the battery life is not such an issue since they are using it anyway. Battery life is really an issue if you are waking the device to process the updates.

这篇关于异步套接字和“静默”断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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