title: recv() fails to read last chunk
body: <p>The function below reads incoming data perfectly but only if the <strong>last</strong> data chunk  is <strong>smaller</strong> than <code>BUFFSIZE</code>. If the size of the last <code>buff</code> happens to be equal to <code>BUFFSIZE</code>, then the program tries to <code>recv</code> again in the next loop iteration and sets <code>bytes</code> to 1844674407379551615 (most probably integer overflow) and repeats this in an infinite loop... Why? Why isn't it 0? And why doesn't it escape the loop at this stage?</p>
  <pre><code>std::string getMsg (int clientFileDescriptor, int timeout_sec)
  {
      std::string msg;
      msg.reserve(BUFFSIZE * 10);
  
      char buff[BUFFSIZE + 1];
  
      signal(SIGPIPE, SIG_IGN);
  
      //set a timeout for reading and writing
      struct timeval tv;
      tv.tv_sec = timeout_sec;
      tv.tv_usec = 0;
      setsockopt (clientFileDescriptor, SOL_SOCKET, SO_RCVTIMEO, (const char*)&amp;tv, sizeof(tv));
  
  
      while (true)
      {
          size_t bytes = recv (clientFileDescriptor, buff, BUFFSIZE, 0);
  
          std::cout &lt;&lt; &quot;bytes read: &quot; &lt;&lt; bytes &lt;&lt; std::endl;
  
          if (0 &lt; bytes &amp;&amp; bytes &lt;= BUFFSIZE)
          {
              msg.append(buff, bytes);
  
              //'buff' isn't full  so this was the last chunk of data 
              if (bytes &lt; BUFFSIZE)
                  break;
          }
          else if (!bytes) //EOF or socket shutdown by the client
          {
              break;
          }
          else
          {
              if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 
                  continue;
              
              //We cannot continue due to other erros (e.g. EBADF/socket not available)
              break;
          }
      }
  
      return msg;
  }
  </code></pre>