title: how to correctly use recv() sys call
body: <p>I am in the process of writing a TCP server using Berkely SOCKET API under linux. Now the clients have a set of specifications and each message send by the client is based on those specifications i-e the message sent from the client corresponds to one of the many structs specified by the specs. Now the scenario is that the server doesn't know which message the client is going to send at what time. Once we get the message we can figure out what it is but not before that. The messages sent by the clients have variable lengths so we can not know in advance what message we are going to get. To solve this I have used the following method:</p>
  
  <pre><code>const char *buf[4096] = { 0 };
            if ( recv (connected, buf, 4096, 0) == -1)
               {
                 printf ("Error in recvng message\n");
                 exit (-1);
               }
  </code></pre>
  
  <p>That is I use a default buffer of size 4096 (no message from the client can be larger than this size). receive in that buffer and then afterwards I Check the message type and take the corresponding action as follows:</p>
  
  <pre><code>struct ofp_header *oph;
  oph=(struct ofp_header *)buf;
  switch (oph-&gt;type)
  {
  case example_pkt:
  handle_example_pkt();
  break;
  }
  </code></pre>
  
  <p>This works fine but I just wanted to confirm that is it an appropriate method or is there something else that could be better than this. All help much appreciated.</p>
  
  <p>Thanks.</p>