简体   繁体   English

Android上的Java-奇怪的套接字行为以及InputStream超时错误

[英]Java on Android - Strange socket behavior with InputStream timeout errors

I have an app which uses an instance of the Socket class to communicate with a server. 我有一个使用Socket类实例与服务器通信的应用程序。 I use the streams returned by socket.getInputStream() and socket.getOutputStream() to read and write data. 我使用socket.getInputStream()socket.getOutputStream()返回的流来读取和写入数据。

When my Android app is always "active" (not minimized), there is no problem with the communication. 当我的Android应用始终处于“活动状态”(未最小化)时,通信就没有问题。 It does not matter how long the connection lasts. 连接持续多长时间都没有关系。

When I "pause" the application and re-open it quickly, everything still works fine. 当我“暂停”应用程序并快速重新打开它时,一切仍然正常。 However, when I pause the application for about 5 minutes and re-open it, the InputStream shows strange behavior: it stops reading anything. 但是,当我将应用程序暂停约5分钟并重新打开时, InputStream表现出奇怪的行为:它停止读取任何内容。 I get timeout errors instead of the data sent by the server. 我收到超时错误,而不是服务器发送的数据。

The connection is still alive, the server is able to write and read. 连接仍然有效,服务器可以写入和读取。 isInputShutdown() on the client-side returns false. 客户端的isInputShutdown()返回false。

Using a network analysis tool, I can also see that the data sent by the server IN FACT reaches the client but it somehow does not get recognized by the InputStream ... 使用网络分析工具,我还可以看到服务器IN FACT发送的数据已到达客户端,但InputStream无法以某种方式识别它。

However, writing data from the client to the server using the OutputStream works fine. 但是,使用OutputStream将数据从客户端写入服务器的效果很好。

Maybe it's worth mentioning that the socket object and the streams are declared as static to be accessible for all the activities of the app. 也许值得一提的是,套接字对象和流被声明为static ,可用于应用程序的所有活动。 But as I don't have any problems with the OutputStream , I cannot imagine that this could be the reason. 但是由于我在OutputStream没有任何问题,所以我无法想象这可能是原因。

The only workaround I have at this point is to close the whole socket and connect a new one to the server. 此时,我唯一的解决方法是关闭整个套接字并将新的套接字连接到服务器。 But this is causing unnecessary network traffic because I have to handshake again. 但这会导致不必要的网络通信,因为我必须再次握手。 It would be better not to do it this way. 最好不要这样。

If anyone has had similar experience and found a solution, I would be really happy if you could share it with me. 如果有人有类似的经验并找到了解决方案,请与我分享,我将非常高兴。

您应该创建将在后台运行的服务,并实现与服务器的套接字连接。

As Kevin Krumwiede pointed out by referring to this post: Strange behavior of socket outputstream android , when sending data every X minutes (eg 3 or 4), everything still works as it should even after 30 minutes of being 'paused'. 正如Kevin Krumwiede指出的那样: 套接字输出流android的奇怪行为,每隔X分钟(例如3或4分钟)发送数据时,即使“暂停”了30分钟后,一切仍然可以正常工作。

I had the hope that Socket.setKeepAlive(true) would be enough to keep the connection alive so I dont have to cause too much unnecessary network traffic but in my particular case, this does not help. 我希望Socket.setKeepAlive(true)足以使连接保持活动状态,因此我不必造成太多不必要的网络流量,但是在我的特定情况下,这没有帮助。

Sending 1 byte of 'garbage' every X minutes 'solves' the problem. 每X分钟发送1字节的“垃圾”可“解决”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM