Android 客户端套接字仅在使用 3G 连接而非 wifi 连接时随机关闭
我有一个在专用服务器上在线运行的Java网络服务器,还有一个用Android SDK和java编写的Android网络客户端。
如果 Android 设备连接 WIFI,则工作正常,但如果我使用 3G 连接设备,当我从客户端调用:readInt()
时,套接字将关闭。这不是在第一次通话时发生的,而是稍后发生的。它看起来非常随机,并且很难解释,但是当没有更多数据读取(或不再读取)时,可能会发生这种情况。例外情况是 SocketException 连接被对等方重置
。
我想更具体地解释一下,但它很难调试,而且在我看来,它看起来像一个 Android bug(对此不确定)。这就是我问这个问题的原因。因为肯定有人遇到过同样的问题。
Android 设备的 WIFI 连接和 3G 连接之间有什么区别,会产生这样的断开连接?有什么办法可以解决这个问题?
谢谢如果有人可以帮忙。
编辑:我的 3G 连接没有中断。是插座在响,而且只是插座在响。只有当我连接 3G 时才会出现这种情况。不在WIFI下。
I have a Java network server running online on a dedicated server, and I have an Android network client written with Android SDK and java.
It's working fine if the android device is connected with WIFI, but if I connect the device using 3G, the socket is closed when I call: readInt()
from the client. It's not happening at the first call, but later. It looks very random, and it's hard to explain, but it may happen when there is no data too read any more (or not). The exception is SocketException connection reset by peer
.
I would like to explain more specifically, but it is very hard to debug, and to my point of view, it looks like an Android bug (not sure about that). And that's why i'm asking this question. Because for sure someone has encountered the same problem.
What could be the difference between a WIFI connection and a 3G connection for an android device, that would produce such a disconnection ? What could possibly be a fix for this ?
Thanks if anyone can help.
Edit: My 3G connection is NOT breaking. It's the socket that is beaking, and only the socket. And it only happens if I connect in 3G. Not in WIFI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的连接可能会在中间中断,在这种情况下您之前创建的套接字已损坏/关闭。
我建议您监听网络更改意图“android.net.conn.CONNECTIVITY_CHANGE”,并在每次网络发生变化时重置套接字。这应该可以解决您的问题
Your connection might be breaking in between, in that case the socket you created before is corrupted/closed.
I would suggest you to listen for Network change intent "android.net.conn.CONNECTIVITY_CHANGE" and reset your socket everytime there is a change in network.This should fix your issue
我找到了答案:一些 3G 网络提供商为了赚取更多利润而关闭了客户的插座。唯一的解决办法是人为地减慢交通速度。
I found the answer: Several 3G network providers close their customers' sockets in order to make more profit. The only solution is to artificially slow down the traffic.