0

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?

jpc
  • 129
  • 3
  • 16
  • 4
    This is the COMPLETELY wrong way to implement an HTTP client. YOU MUST PARSE THE RESPONSE CORRECTLY in order to know what format it is in, which will then dictate HOW you call `recv()` and WHEN to stop calling it. A simple `recv()` loop like this will not suffice. – Remy Lebeau Sep 04 '19 at 00:16
  • @RemyLebeau How does one correctly parse the response, and what about the recv() call can change? – jpc Sep 04 '19 at 00:17
  • 1
    @jpc see the duplicate answers I just linked this question to. – Remy Lebeau Sep 04 '19 at 00:21
  • 3
    @Nina there is no `Content-Length` header if a non-identity `Transfer-Encoding` header is used instead. You have to analyze the HTTP headers to know what the actual transfer format of the body is, and then read it accordingly. Some formats are self-terminating in the data itself (chunking, MIME) and some are not (Content-Length, EOF by disconnect). – Remy Lebeau Sep 04 '19 at 00:22
  • @jpc You have to actually implement the HTTP protocol to make any sense of the data you receive. This is a very non-trivial problem and you should probably start by looking at an HTTP client someone else made and understanding it or reading the HTTP specification. The protocol specification tells you when you have the entire response, and it's not simple and varies with the encoding used. Perhaps start with something simpler than HTTP 1.1 such as HTTP 0.9 or 1.0 since they're much simpler. – David Schwartz Sep 04 '19 at 05:17

0 Answers0