Search code examples
cloopseofinfinitefgetc

C fgetc() on socket/file stream while checking for EOF goes into infinite loop


int main(int argc, char *argv[]) {

    int conn_s = socket(AF_INET, SOCK_STREAM, 0);
    struct addrinfo hints;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    struct addrinfo *addr = (struct addrinfo *) calloc(1, sizeof(struct addrinfo));

    getaddrinfo("google.com", "80", &hints, &addr);

    connect(conn_s, addr->ai_addr, sizeof(struct sockaddr_in));

    char *http_request = "GET / HTTP/1.1\n\n";

    send(conn_s, http_request, strlen(http_request), 0);
    FILE *sockfile = (FILE *) fdopen(conn_s, "r");
    FILE *fp = fopen("/Users/leekaiwei/Desktop/results.html", "w+");
    int ch;

    while ((ch = fgetc(sockfile)) != EOF) {
        fprintf(fp, "%c", ch);
    }

    close(conn_s);
    fclose(fp);
    fclose(sockfile);
    free(addr);
    return 0;
}

The while loop never ends. Why is this? I have done this with a local file and it works fine. It also works fine with a for loop, it's just that the fgetc() never returns EOF.


Solution

  • HTTP/1.1 defaults to connection: keepalive, so like simonc said, the server is actually waiting for you to make the next request, and will wait until a server-dependent timeout. Use HTTP/1.0 to get EOF.

    FWIW: it took 245 seconds to complete.