title: How to add heartbeat messaging on top of this Java code( for KnockKnockClient/Server)?
body: <p>I'm studying the following basic Java socket code( <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="noreferrer">source</a> ). It's a Knock-Knock-Joke client/server app. </p>
  
  <p>In the <a href="http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java" rel="noreferrer">Client</a>, we set  up the socket as usual:</p>
  
  <pre><code>try {
    kkSocket = new Socket("localhost", 4444);
    out = new PrintWriter(kkSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
  } catch( UnknownHostException uhe ){ /*...more error catching */
  </code></pre>
  
  <p>And then later, we just read and write to Server:</p>
  
  <pre><code>BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  String fromServer;
  String fromUser;
  
  while ((fromServer = in.readLine()) != null) {
    System.out.println("Server: " + fromServer);
    if (fromServer.equals("bye."))
        break;
  
    fromUser = stdIn.readLine();
  
    if (fromUser != null){
        System.out.println("Client: " + fromUser);
        out.println(fromUser);
    }
  </code></pre>
  
  <p>And on the <a href="http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockServer.java" rel="noreferrer">server</a>, we have the corresponding code, to get the joke punch-line.</p>
  
  <pre><code>    KnockKnockProtocol kkp = new KnockKnockProtocol();
  
      outputLine = kkp.processInput(null);
      out.println(outputLine);
  
      while ((inputLine = in.readLine()) != null) {
           outputLine = kkp.processInput(inputLine);
           out.println(outputLine);
           if (outputLine.equals("Bye."))
              break;
  </code></pre>
  
  <p>I want to attach a heartbeat to the whole thing, which will print out to the console whenever it detects that the other side died. Because what happens now if I kill the other side is an exception - like this one below:</p>
  
  <p><img src="https://i.stack.imgur.com/H611f.png" alt="enter image description here"></p>
  
  <p>So if I am running both KnockKnockClient and KnockKnockServer, then I shut down KnockKnockServer, what should happen is that on the Client I see this outputted:</p>
  
  <pre><code>&gt;The system has detected that KnockKnockServer was aborted
  </code></pre>
  
  <p>I'm looking for any tips.  So far I've mainly been trying to run a daemon thread that periodially creates new connections to the other side. But I'm confused about what condition to check for(but I think it's just a <code>boolean</code> value?).  Is that the right approach? I just found out online there's a library called <a href="http://www.jgroups.org/" rel="noreferrer">JGroups for multicast</a> networking - would that be a better way? I'm looking for any tips.</p>
  
  <p><a href="http://pastebin.com/cDYbgxCZ" rel="noreferrer">My server-code so far</a>(sorry it's messy)</p>
  
  <p>&amp;</p>
  
  <p><a href="http://pastebin.com/53vYaECK" rel="noreferrer">Client-side</a></p>
  
  <p>thanks</p>