Android Question Persistent TCP connection

jgbozza

Member
Licensed User
Longtime User
There is a server device which a tablet must be connected permanentely. However, the server may be turned down due to electricity cuts. When it is up again, the previous socket at the client end (in the android tablet) is still opened but is no longer working.
I would like to create a service to "ping" this server and in case there is no response, my app closes the previous socket and opens a new one.
The drawback is that the connection process takes some seconds to be established once the socket.connect function is called and if the ping procedure detects that there is no connection it will close and open the socket again and this will be an infinite loop.
What flags do I have in order to see if there is a valid connection? socket.isconnected does not seem to work, neither Socket.isinitialized what leads me to the only solution available. Closing the previous socket and open a new one.

The poor solution I found was create timers to manage the connection and several boolean flags to inform that there is a connection in process and so on....probably there are other more inteligent workarounds for this issue.

Any clues ?
 
Top