title: Socket Set TimeOut make app crash / Try reconnect is connection is lost
body: <p>I'm doing a project for receiving data from a server. This code works ok until I add the soTimeout line for the socket. </p>
  
  <pre><code>package com.lumineria.mywatertankr3;
  
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.os.Handler;
  import android.support.v7.app.AppCompatActivity;
  
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.content.Intent;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.SocketTimeoutException;
  
  
  public class MainActivity extends AppCompatActivity {
  
  public static final String SETTINGS_PREF = "MyTankSettings";
  
  TextView textViewNivel,textViewEstado;
  String strNivel = "";
  String ipAddressSaved="";
  
  // Create the Handler object (on the main thread by default)
  Handler handler = new Handler();
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_main);
  
      textViewNivel = findViewById(R.id.nivel);
      textViewEstado = findViewById(R.id.status);
  
      // Restore preferences
      SharedPreferences settings = getSharedPreferences(SETTINGS_PREF, 0);
  
      // Levanto de memoria los valores de IP y Tiempo de Refresco
       ipAddressSaved = settings.getString("ip", "");
  
      // SI tengo un valor definido de IP intento la conexion
      if (!ipAddressSaved.equals(""))
      {
          Thread clientThread = new Thread(new ClientThread());
          clientThread.start();
      } else {
          Toast.makeText(getApplicationContext(),
                  "No se definio IP del Server",
                  Toast.LENGTH_LONG).show();
      }
  
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_main, menu);
      return super.onCreateOptionsMenu(menu);
  }
  
  
  public class ClientThread  implements Runnable  {
      public void run() {
          try {
              while (true) {
                  InetAddress serverAddr = 
  InetAddress.getByName(ipAddressSaved);
                   Socket socket = new Socket(serverAddr, 10000);
  // WHEN I ADD THIS LINE APP CRASHES
                  // socket.setSoTimeout(10000);
  
  
  
                  InputStreamReader reader = new InputStreamReader (socket.getInputStream());
                  BufferedReader in = new BufferedReader( reader);
                  String line;
  
  
  
  
                  while ((line = in.readLine()) != null) {
                     strNivel = line;
                      handler.post(new Runnable() {
                          @Override
                          public void run() {
                              textViewNivel.setText(strNivel);
                          }
                      });
  
                  }
                  in.close();
                  socket.close();
                  Thread.sleep(500);
  
              }
          } catch (SocketTimeoutException exception)  {
              textViewEstado.setText(textViewEstado.getText()+"1");
          } catch (IOException exception)  {
              textViewEstado.setText(textViewEstado.getText()+"2");
          } catch (Exception exception){
              textViewEstado.setText(textViewEstado.getText()+"3");
          }  finally {
              textViewEstado.setText(textViewEstado.getText()+"4");
          }
      }
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.action_settings:
              Intent intent=new Intent(this,SettingsActivity.class);
              startActivity(intent);
              return true;
  
          default:
              return super.onOptionsItemSelected(item);
      }
  }
  
  
  
  }
  </code></pre>
  
  <p>What I'm doing wrong? Is there a best way to check if server is no longer available? I want to check when nothing more is coming I prefer to disconnect and restart the connection.</p>
  
  <p>THanks in advance</p>