title: C++ Socket API "Heartbeat"
body: <p>I'm trying to make a simple heartbeat check from client to server and vice-versa, if connection on either is broken off unexpectedly it prints a message and calls closesocket.</p>
  <p>I spent 8 hours on this and it still isn't acceptable to my mentor. Right now I got something that works, but if breakpoint is placed before while loop and connected client is forcefully closed, trying to go past breakpoint causes crash when it should break the loop and write out error.</p>
  <p>Server side code:</p>
  <pre class="lang-cpp prettyprint-override"><code>int main(int argc, char *argv[])
  {
      SOCKET s, sa;
      WSAData oWSAData;
      WORD wVersion = 0x0001;
      WSAStartup(wVersion, &amp;oWSAData);
      
      s = socket(AF_INET, SOCK_STREAM, 0);
      sockaddr_in srv_address;
      memset(&amp;srv_address, 0, sizeof(srv_address));
      srv_address.sin_family = AF_INET;
      srv_address.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
      srv_address.sin_port = htons(1099);
      
      bind(s, (sockaddr*) &amp;srv_address, sizeof(srv_address));
      int l = listen(s, 10);
      
      if (l &lt; 0)
          printf(&quot;Listen error\n&quot;);
      else
      {
          printf(&quot;Listen OK. Listening on port %u\n&quot;,
                 htons(srv_address.sin_port));
          
          sa = accept(s, NULL, NULL);
          
          while (true)
          {
              char buffer[1000];
              
              int nRecvLen = recv(sa, buffer, 999, 0);
              buffer[nRecvLen] = '\0';
              int r = recv(sa, NULL, 0, 0);
              if (r == SOCKET_ERROR &amp;&amp; WSAGetLastError() == WSAECONNRESET)
              {
                  printf(&quot;Konekcija je naglo prekinuta!\n&quot;);
                  break;
              }
              else
              {
                  if (nRecvLen &gt; 0)
                  {
                      for (int i = 0; i &lt; nRecvLen; i++)
                      {
                          cout &lt;&lt; buffer[i];
                      }
                  }
              }
          }
          closesocket(sa);
          closesocket(s);
      }
      
      WSACleanup();
      return 0;
      
  }
  </code></pre>
  <p>and client side:</p>
  <pre class="lang-cpp prettyprint-override"><code>int main()
  {
      SOCKET s;
      WSAData oWSAData;
      WORD wVersion = 0x0001;
      WSAStartup(wVersion, &amp;oWSAData);
      
      s = socket(AF_INET, SOCK_STREAM, 0);
      sockaddr_in srv_address;
  
      memset(&amp;srv_address, 0, sizeof(srv_address));
      srv_address.sin_family = AF_INET;
      srv_address.sin_addr.S_un.S_un_b.s_b1 = xxx;
      srv_address.sin_addr.S_un.S_un_b.s_b2 = xxx;
      srv_address.sin_addr.S_un.S_un_b.s_b3 = x;
      srv_address.sin_addr.S_un.S_un_b.s_b4 = xxx;
      srv_address.sin_port = htons(1099);
      
      int c = connect(s, (sockaddr*) &amp;srv_address, sizeof(srv_address));
      
      if (c &lt; 0)
      {
          printf(&quot;Connection error\n&quot;);
          cout &lt;&lt; (WSAGetLastError());
      }
      else
      {
          string l = &quot;Heartbeat\n&quot;;
          int p = l.size();
          char buff[1000];
          strcpy_s(buff, l.c_str());
          printf(&quot;Connected\n&quot;);
          
          while (true)
          {
              if (send(s, buff, p, 0) &gt; 0)
              {
                  Sleep(1000);
              }
              else
              {
                  printf(&quot;Konekcija je naglo prekinuta\n&quot;);
                  shutdown(s, SD_BOTH);
                  closesocket(s);
                  break;
              }
          }
          WSACleanup();
          return 0;
      }
  }
  </code></pre>