套接字连接和查询。这是在电池寿命方面更好的解决方案? [英] Socket connections and Polling. Which is a better solution in terms of battery life?

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

问题描述

所以...我正在为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) Opening and Closing the connection every time a command is sent, if the chat tab is opened then keep the connection constant.

B)保持连接不断的所有的时间。

b) Keep the connection constant all the time.

我已经采取了网上到处看看,但已经得到了混合答案,有的说保持一个持久连接是不好的电池寿命和其他人说,这是不是(例如:你是问,如果拿着一个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."

或者,如果有另一种解决方案,效果会更好。我不认为谷歌的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连接不与去睡觉计算机之间的交互很好。这个问题的情况是这样的:你的Andr​​oid用户将自己的Andr​​oid设备,而你的应用程序运行时睡觉,然后远程用户的程序(或任何为在TCP连接的另一端)在TCP流中发送一些数据。远程用户的计划从来没有得到任何的ACK回来从Android设备,因为课程的Andr​​oid设备处于睡眠状态,所以远程设备的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连接,你的一部分,规定设备是持续到睡眠现在程序。

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的逻辑,但我只用安卓一点,这样可能只是无知的我的一部分。这可能是值得测试的,至少。

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连接是开放的,而且确实使用了一些CPU周期和天线力量......但默认时间间隔在Android上发送keepalive报文是2小时,所以我怀疑用于电源将是明显的。

(*) 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天全站免登陆