套接字连接和轮询.就电池寿命而言,哪个是更好的解决方案? [英] Socket connections and Polling. Which is a better solution in terms of battery life?

查看:27
本文介绍了套接字连接和轮询.就电池寿命而言,哪个是更好的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我正在为 Android 制作一个应用程序.应用程序需要发送和接收实时聊天数据(需要是一个套接字),但它也需要发送命令(客户端不知道它何时发送某些东西).

So... I'm making an application for Android. The application needs to send and receive realtime chat data (needs to be a socket) but it also needs to send commands (which don't as the client knows when it is sending something).

我需要知道在节省用户电池方面什么是更好的解决方案.

I need to know what is a better solution in terms of saving the user's battery.

a) 每次发送命令时打开和关闭连接,如果打开聊天选项卡,则保持连接不变.

a) Opening and Closing the connection every time a command is sent, if the chat tab is opened then keep the connection constant.

b) 始终保持连接不变.

我浏览了互联网,但得到的答案不一,有人说保持持久连接不利于电池寿命,而另一些人则说不会(例如:你问是否持有TCP 连接打开会耗尽电池寿命?也许我已经离开这里了,但是,保持连接打开不应该浪费电池寿命......如果你认为它会我很想知道你从哪里得到这些信息.听起来很奇怪给我.")

I've taken a look around the internet but have gotten mixed answers, some say keeping a persistent connection is bad for battery life and others say that it isn't (Example: "Are you asking if holding a TCP connection open will drain battery-life? Maybe I am WAY off here but, holding a connection open shouldn't waste battery life... If you think it will I would love to know where you got that information. It sounds SO strange to me.")

或者如果有其他更好的解决方案.我也不认为 Google 的 C2DM 在这种情况下会很有用.

Or if there is another solution that would be better. I don't think that Google's C2DM would be very useful at all in this situation either.

基本上,什么更耗电:保持持续连接,或者除非打开聊天选项卡,否则打开和关闭连接?

Basically, what drains the battery more: having a persistent connection, or opening and closing the connection unless the chat tab is open?

谢谢!

推荐答案

保持空闲 TCP 套接字连接打开(不发送或接收数据)不会(或至少不应该)消耗比拥有更多的电池电量关闭.那是因为空闲的 TCP 连接不使用带宽或 CPU 周期(*).

Keeping an idle TCP socket connection open (with no data being sent or received) will not (or at least, should not) consume any more battery than having it closed. That is because an idle TCP connection uses no bandwidth or CPU cycles(*).

也就是说,长时间保持 TCP 连接打开对于移动设备来说可能不是一个好的选择,因为 TCP 连接不能与进入睡眠状态的计算机很好地交互.问题场景是这样的:当您的应用程序运行时,您的 Android 用户将他的 Android 设备置于睡眠状态,然后远程用户的程序(或 TCP 连接另一端的任何程序)通过 TCP 流发送一些数据.远程用户的程序永远不会从 Android 设备收到任何 ACK,因为 Android 设备当然处于睡眠状态,因此远程设备的 TCP 堆栈假定它发送的 TCP 数据包肯定已经丢失,并通过增加其超时时间来响应,减少它的 TCP 窗口大小(又名 TCP 数据包数量),并重新发送 TCP 数据包.但是Android设备仍然处于休眠状态,因此同样的事情再次发生.结果是几分钟后,TCP 连接的远程端的速度减慢到即使 Android 设备唤醒,TCP 连接也可能太慢而无法使用——此时您的程序将需要关闭陷入困境的 TCP 连接并无论如何都要启动一个新的连接,那么为什么还要尝试保持打开状态呢?

That said, keeping a TCP connection open for extended periods may not be a good option for a mobile device, because TCP connections don't interact well with computers that go to sleep. The problem scenario would be this: your Android user puts his Android device to sleep while your app is running, and then the remote user's program (or whatever is at the other end of the TCP connection) sends some data over the TCP stream. The remote user's program never gets any ACKs back from the Android device, because of course the Android device is asleep, so the remote device's TCP stack assumes that the TCP packets it sent must have been lost, and it responds by increasing its timeout period, decreasing its TCP window size (aka number-of-TCP-packets-allowed-in-flight-at-once), and resending the TCP packets. But the Android device is still asleep, and thus the same thing happens again. The upshot is that a few minutes later, the remote end of the TCP connection has slowed down to the point where even if the Android device was to wake up, the TCP connection will likely be too slow to be usable -- at which point your program will need to close the bogged-down TCP connection and start up a fresh one anyway, so why bother trying to keep it open?

所以我的建议是使用选项 (a),并规定您关闭 TCP 连接作为您的 device-is-going-to-sleep-now 例程的一部分.

So my recommendation would be to go with option (a), with the stipulation that you close the TCP connection as part of your device-is-going-to-sleep-now routine.

一个可能的警告是,如果 Android 具有保持 TCP 连接打开的功能,会导致 WiFi 或蜂窝网络硬件在本来可以进入睡眠状态的情况下保持通电——如果是这种情况,那么 Android 设备将支付电池成本来为天线供电,否则它就不必支付.我不知道任何类似的 Android 逻辑,但我只使用了一点 Android,所以这可能只是我的无知.至少,它可能值得测试.

One possible caveat would be if Android has a feature where keeping a TCP connection open causes the WiFi or cell-network hardware to remain powered up in a situation where it could otherwise be put to sleep -- if that is the case, then the Android device would pay a battery cost for powering the antenna, which it wouldn't otherwise have had to pay. I'm not aware of any Android logic like that, but I've only used Android a little so that might just be ignorance on my part. It might be worth testing for, at least.

(*) 好吧,从技术上讲,当 TCP 连接打开时,TCP 确实会每隔一段时间发送一个keepalive"数据包,这确实会使用一些 CPU 周期和天线功率……但是在 Android 上发送 keepalive 数据包的默认时间间隔是 两个小时,所以我怀疑用于此的功率会很明显.

(*) Well, technically TCP does send a "keepalive" packet every so often while a TCP connection is open, and that does use some CPU cycles and antenna power... but the default interval for sending keepalive packets on Android is two hours, so I doubt the power used for that would be noticeable.

这篇关于套接字连接和轮询.就电池寿命而言,哪个是更好的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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