title: recv() until a NUL byte is received?
body: <p>I'm trying to receive a single packet at a time from the server, since packets are going too fast, and each is of undefined size, calling recv() with number of bytes to read will read the first packet and maybe a part of the second packet.  Since each packet is NULL terminated, I thought reading byte by byte until a NULL byte is received.</p>
  
  <pre><code>    int recvLen = 0;
      char TB;
      char recvBuffer[1024];
      while (recv(Socket, &amp;TB, 1, 0) &gt; 0 &amp;&amp; TB != 0 &amp;&amp; recvLen &lt; 1024)
      {
          recvBuffer[recvLen] = TB;
          recvLen++;
      }
  </code></pre>
  
  <p>I don't think this method is efficient at all.  If the server sent 1024 bytes, <code>recv()</code> will be called 1024 times.</p>
  
  <p>Is there any other method to recv() until a NULL char is received, or some better method than this one I'm using?</p>
  
  <hr>
  
  <p>EDIT:
  i added the packet size infront of the data sent from the server, but now, if a false packet or even sometimes for no reason, packets gets messed up and no correct data is received. here is my code</p>
  
  <pre><code>#define UPLOAD_LEN 2755
  int PacketSize, recvLen;
  char Size[4];
  char recvBuffer[UPLOAD_LEN+1];
  while(1)
  {
      if(recv(Socket,Size,4,0)&gt;0)
      {
          Size[4] = '\0';
          PacketSize = atoi(Size);
          if (PacketSize &gt; UPLOAD_LEN || PacketSize &lt;= 0) continue;
          recvLen = recv(Socket, recvBuffer, PacketSize, 0);
      } else recvLen = -1;
      if (recvLen &gt; 0)
      {
          recvBuffer[recvLen] = '\0';
          ProcessData(recvBuffer);
      }
      else
      {
          closesocket(Socket);
      }
  }
  </code></pre>