title: How to retry "try-catch" in a finite number and delay it?
body: <p>I use this code to transfer data between two device.</p>
  
  <pre><code>public void run() {
  
          try {
              Socket s = new Socket(myIpAddress, SERVERPORT);
  
              // outgoing stream redirect to socket
              OutputStream out = s.getOutputStream();
  
              PrintWriter output = new PrintWriter(out);
              output.println("DLS");
              BufferedReader input = new BufferedReader(new InputStreamReader(
                      s.getInputStream()));
  
              // read line(s)
              String st = input.readLine();
              // . . .
              // Close connection
              s.close();
  
          } catch (UnknownHostException e) {
              run();
              e.printStackTrace();
          } catch (IOException e) {
              run();
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>My problem is i have to start the server first and after that, i can start the client for a successful connection. </p>
  
  <p>In the</p>
  
  <pre><code>public void run()
  </code></pre>
  
  <p>I put <code>run()</code> to the <code>UnknownHostException</code> and to the <code>IOException</code>.
  It works fine in an other code but here i get the <code>StackOverFlowError</code>.
  How can i make it to RE-try in a finite number and only try every X second?</p>
  
  <p>I tried this way because the <code>run()</code> and <code>Thread.sleep(5000);</code> cried for the unhandled exceptions. When i start the service, the app is froze.</p>
  
  <pre><code>public void run() throws UnknownHostException, IOException {
  
          Socket s = new Socket(myIpAddress, SERVERPORT);
  
          // outgoing stream redirect to socket
          OutputStream out = s.getOutputStream();
  
          PrintWriter output = new PrintWriter(out);
          output.println("DLS");
          s.close();
  
      } 
  
  @Override
  public void onCreate() {
      for (int i = 0; i &lt; 10; i++) {
          try {
              run();
              break; // no exception: break out of the loop.
          }
          catch (IOException e) {
              try {
                  Thread.sleep(5000);
              } catch (InterruptedException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
          }
      }
  </code></pre>