Android: Server disconnects app after device sleeps. But Java socket never detects disconnection when device wakes up -


in android app have written service connects server. calling java socket .getoutputstream().write , .getinputstream().read write requests , read responses , server. plain , simple that.

everything works fine until device sleeps.

when device sleeps, server disconnects app connection. when device wakes up, app service starts running. never detects disconnected connection. .getinputstream().read keeps waiting responses forever , .getoutputstream().write writes requests socket successfuly. weird.

thus java, socket normal though disconnected server. , there no way detect same.

ideally, .getinputstream().read should return -1 after device wakes , service starts running. doesn't happen. neither .getoutputstream().write throws ioexception. , user has way stop application forcefully through app settings, not @ acceptable app perspective.

what tried?

  1. i have gone through relevant question this, this , this answers there suggests write or read socket check if still connected (like heartbeats suggested here). however, stated doesn't work in case (i.e. socket read keeps waiting , write doesn't throw exception, if socket still connected)

  2. as workaround thought of closing , reconnecting connection compulsory (regardless connected or not) after device wakes up. couldn't find specific intent device wake up. (some posts on suggested android.intent.action.screen_on device wake up. don't want reset connection every time when screen turned on. no, has device wakeup intent.)

i badly stuck , know how handle situation in many other apps stay connected servers.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -