Question

I'm developing an Android app that requires to has a permanent connection to some server, I made that connection using socket programming, the problem is when the server lost the connection or resets the connection my app crashes, So, how to handle such connections cut off?

Was it helpful?

Solution

I am assuming that you are programming your socket methods inside a Thread/AsyncTask/extends System Class, if you are not, start doing it, because you shouldnt use blocking methods in the UI class(read/write/connect).

For your problem simply surround your socket statements with a try/catch block. If an exception occurs i.e connection timed simply reset your socket and call the connect method inside the catch block.

If you want me to be more specific you'll have to post your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top