title: Java Socket Heartbeats
body: <p>I am developing a program that uses sockets and I want to send a heartbeat to my clients every second in order to check if they are still connected.</p>
  
  <p>How do I do this efficiently?</p>
  
  <pre><code>                    while (true) { // send heartbeats to client
                      try {
                          socketOut.write(0);
                      } catch (Exception e) {
                          System.out.println("client disconnected");
                          break;
                      }
                  }
  </code></pre>
  
  <p>That is my current code, however the while loop messes up all my other clients.  It makes them unable to connect to the game (the server socket).  How do I accomplish heartbeats without affecting the game and other clients?</p>
  
  <p><strong>Note:</strong>  I've debating using a timer to achieve my 1 second delays, but I do not know if it will work nor if it is efficient for a large game with many clients connected at once.</p>