title: Detect if connection is failed - Java Android
body: <p>How to check if server is online or offline, and if is offline start connecting until  server is on. I have tried with this:</p>
  
  <pre><code>        connectBtn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              new Thread(rConnection).start();
  
          }
      });
  
     public Runnable rConnection = new Runnable() {
  
      @Override
      public void run() { 
          boolean status = connect();         
          while (!status)
          {
              System.out.println("Connection Status: " + status);
              status = Connect();
          }
      }
   };
  
  public boolean Connect() {
  
          boolean status = false;
  
          try {
              s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
  
              System.out.println("Socket: " + s.toString());
  
              if (s.toString() != "")
              {
                  status = true;
              }
          } catch (UnknownHostException e) {
              e.printStackTrace();
              status = false;
              s=null;
          } catch (IOException e) {
              e.printStackTrace();
              status = false;
              s=null;
          } catch (NullPointerException e)
          {
              e.printStackTrace();
              status = false;
              s=null;
          }
  
          return status;
      }
  </code></pre>
  
  <p>If server is running before staring app it connects successfully but if server is off or disconnects after some time I don't get any error message and it won't start reconnecting again. How to solve this?</p>