title: How do I know if the request was received completely using TCP sockets?
body: <p>I have a multi-thread HTTP TCP server (written in Linux) and below is the function, that handles the client:</p>
  
  <pre><code>    void tcp_server::connection_handler(client* cl) {
      // add client to the clients &lt;vector&gt;
      mx.lock();
  
      cl-&gt;set_id(static_cast&lt;int&gt;(clients.size()));
      clients.push_back(*cl);
      cout &lt;&lt; "New client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; " has been added to the clients list" &lt;&lt; endl &lt;&lt; endl;
  
      mx.unlock();
  
      char read_buffer[1024];
      ssize_t message_size;
      string received_message;
      while (accept_connections) {
          memset(&amp;read_buffer, 0, sizeof(read_buffer));
          received_message.clear();
  
          message_size = recv(cl-&gt;sock, read_buffer, sizeof(read_buffer) - 1, 0);
  
          if (message_size == 0) {
              // client disconnected
              cout &lt;&lt; "Client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; " has been disconnected" &lt;&lt; endl;
              close(cl-&gt;sock);
  
              //remove client from the clients &lt;vector&gt;
              mx.lock();
  
              int client_index = find_client_index(cl);
              clients.erase(clients.begin() + client_index);
              cout &lt;&lt; "Client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; " has been removed from the clients list" &lt;&lt; endl;
  
              mx.unlock();
  
              delete cl;
  
              break;
          }
          if (message_size == -1) {
              if (!accept_connections) {
                  cerr &lt;&lt; "Unable to receive message from client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; ". Server has been shut down. Stop receiving messages from this client" &lt;&lt; endl;
              }
              else {
                  cerr &lt;&lt; "Error while receiving message from client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; endl;
              }
              cerr &lt;&lt; strerror(errno) &lt;&lt; endl;
          }
          else {
              //message was received succesfully
  
              cout &lt;&lt; "[Server] Client's message has been received" &lt;&lt; endl;
              cout &lt;&lt; "[Server] Client's message: " &lt;&lt; endl;
              cout &lt;&lt; "----------------------------" &lt;&lt; endl;
              cout &lt;&lt; read_buffer &lt;&lt; endl;
              cout &lt;&lt; "----------------------------" &lt;&lt; endl;
  
              for (unsigned int i = 0; i &lt; message_size; i++) {
                  received_message.push_back(read_buffer[i]);
              }
  
              string response_message = convert_client_message(received_message);
  
              if (send(cl-&gt;sock, response_message.c_str(), response_message.size(), 0) == -1) {
                  cout &lt;&lt; "[Server] Message sending to client with id " &lt;&lt; cl-&gt;get_id() &lt;&lt; " failed" &lt;&lt; endl;
              }
  
              cout &lt;&lt; "[Server] Server's response: " &lt;&lt; endl;
              cout &lt;&lt; "----------------------------" &lt;&lt; endl;
              cout &lt;&lt; response_message &lt;&lt; endl;
              cout &lt;&lt; "----------------------------" &lt;&lt; endl &lt;&lt; endl;
  
              cout &lt;&lt; "[Server] Message has been sent to client" &lt;&lt; endl &lt;&lt; endl;
              cout &lt;&lt; "============================" &lt;&lt; endl &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>How can you see, there's a buffer, that contains client's message, only is 1024 bytes, when request can be bigger than 1024 bytes.
  So, the problem is: I need to know if request was received completely.
  I was thinking about writing parser that might tell me if a full request was received, but I don't really know how to detect full request. </p>