Hi, I'm using WinSock, and am making a function to receive data. How can I know when I have reached the end of the data that I'm receiving. Like, if I send "GET /" to www.google.com on port 80, I should receive the HTML of the index page. But in my loop that receives, it won't stop until it gets x number of bytes. This results in an infinite loop. How can I tell if a server stops sending me data so I can stop the loop?

bool RecvAll(SOCKET sock, char *data, long size)
{
   long BytesRecv = 0;
   long BytesRecvTemp = 0;

   if (sock == INVALID_SOCKET) return false;

   while (BytesRecv < size)
   {
      BytesRecvTemp = recv(sock, data, size - BytesRecv, NULL);
      if (BytesRecvTemp > 0)
      {
         BytesRecv += BytesRecvTemp;
         data += BytesRecvTemp;
      }
      else if (BytesRecvTemp < 0)
      {
         memset(data, 0, size);
         return false;
      }
   }

   return true;
}

After messing with the program for a while, I found that recv will return 0 if it's done. Is this correct?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.