title: Safely Creating "Heart Beat" over Network (Ping?)
body: <p>I created extended TCP server and TCP client classes (in C#) for communication over network for my project use.</p>
  
  <p>And as far as I understand, a client cannot really know if a server is down unless <em>it requests for something which expects a reply but does not get it</em>.</p>
  
  <p>In our application, time and availability (of the server) are critical factors as it involve heavy machines for automation. Hence, according to the discussion on the design, <strong>the server is supposed to send its "Heart Beat" periodically</strong> such that in case a client does not receive anything from server after a period of time, it will:</p>
  
  <ol>
  <li>Start to attempt its own recovery actions and if it still fails, </li>
  <li>It will raise alarm to the service officer in the control room</li>
  </ol>
  
  <p>I am supposed to implement the "heart beat" part in the server. And I have simple implementation of creating "Heart Beat".</p>
  
  <pre><code>public void SendHeartBeatToAllClients(byte[] hbdata) {
    foreach (Socket socket in clientNoSocketList.Select(x =&gt; x.Value).ToList())
      socket.Send(hbdata);      
  }
  </code></pre>
  
  <p>So far it works fine, but one thing that worries me is that the heart beat data (<code>hbdata</code>) is short (only few pre-arranged bytes, to save time to talk over many machines) and self-defined and since the server also sends some other data besides the <code>hbdata</code>, and considering the possible latency or other unexpected case, there is always a possibility for this <code>hbdata</code> to be mixed up. Also, in my "heart beat" implementation, the client does not need reply anything to the server.</p>
  
  <p>So here are my questions:</p>
  
  <ol>
  <li>Is my worry not well-grounded (as it is fine so far)? Is there any flaw?</li>
  <li>Is <code>Ping</code> a better or a common way to have such heart beat functionality over TCP? Why or why not?</li>
  <li>If <code>Ping</code> is to be implemented, considering that <code>Ping</code> has reply, is there a way to implement replyless <code>Ping</code>?</li>
  <li>Any suggestion to make the heart beat robust enough yet in the shortest amount of data possible?</li>
  </ol>