title: When server connection is lost, how to stop and restart a service of the client on its own?
body: <p>This is the <a href="https://stackoverflow.com/questions/14985678/how-to-keep-the-android-client-connected-to-the-server-even-on-activity-changes">link</a> to my code. On service start a socket a created that sends data to the server from android client. This service is stopped on destroy. </p>
  
  <p>But I noticed, if my client loses connection with the server I have no way of knowing it and worse I cannot reconnect to the server. </p>
  
  <p>I assume the best way would be to receive an echo of the command sent by the client to the server and if there is no reply from server understand that the connection is lost. After knowing that the connection is lost I need to self stop the service and restart it. I have no idea how to go about it. After doing this I need to rebind to the activity. </p>
  
  <p>The other way is the client socket should be opened and closed for every data being sent. This way it automatically reconnects to the server. I do not know how to do this too.</p>
  
  <p>Any help is appreciated</p>