Essentially I am sending a GET request to some URL, and receiving the result using recv()
. I have a while loop that gets chunks of the file from recv()
in a buffer.
void send_GET(int client, char *url) {
char buf[BUF_SIZE] = {0};
sprintf(buf, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", url);
send(client, buf, strlen(buf), 0);
}
int main() {
//sock is a socket()
send_GET(sock, url);
while ((numbytes = recv(sock, buf, BUF_SIZE - 1, 0)) > 0) {
buf[BUF_SIZE] = '\0';
printf("%s", buf);
}
printf("\n");
close(sock);
return 0;
}
Once I get the entire HTML from the page I requested, the recv()
process just idles. I know close(sock)
will end it, but I don't know how to figure out how to tell when the file has ended. I don't know how to get the while loop to exit because numbytes never equals 0 or less than 0. Once it gets the last chunk, it just sits and idles.
How can I tell when the end of the file I requested has been received?