Home > Software design >  Why does my html file only load if it's all on one line? [C Socket Programming]
Why does my html file only load if it's all on one line? [C Socket Programming]

Time:01-05

I started learning socket programming in C recently, and after following a tutorial online, I ended up with this code for a server that loads an html file that can be viewed in a browser:

int main(void) {
    FILE * html_data;
    if ((html_data = fopen("index.html", "r")) == NULL) {
    puts("File Not Found");
    exit(1);
    }

    char response_data[1024];
    fgets(response_data, 1024, html_data);

    char http_header[2048] = "HTTP/1.1 200 OK\r\n\n";
    strcat(http_header, response_data);

    int server_socket = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(4999);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr));

    listen(server_socket, 5);

    int client_socket;
    while (1) {
    client_socket = accept(server_socket, NULL, NULL);
    send(client_socket, http_header, sizeof(http_header), 0);
    shutdown(client_socket, 2);
    }

    return 0;
}

The first time I tried connecting to this server with my browser (Brave Browser), I was able to connect but all I could see was a blank page. I later found out that the only way my html would load is if it were all on one line, like it is here:

<!DOCTYPE html><html><body><p>HELLO WORLD</p></body></html>

Is there any reason why this would be? It's a very odd quirk I'm not sure why it's working like this. I've tried this on Firefox as well to make sure this isn't just a chromium thing. Can someone enlighten me? Thank you.

CodePudding user response:

You need to check the return value of all the functions you call.

Consider setting the socket option SO_REUSEADDR at least for development so you don't have to wait for tcp/ip timeout to be able to start your server again.

Here is an example using sendfile(). read() use a similar calling pattern.

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void) {
    int server_socket = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(4999);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr));
    listen(server_socket, 5);
    for(;;) {
        int client_socket = accept(server_socket, NULL, NULL);
        
        int html_data = open("index.html", O_RDONLY);
        if(html_data == -1) {
            puts("File Not Found");
            // send 403 to client?
            return 1;
        }
        struct stat statbuf;
        if(fstat(html_data, &statbuf) == -1) {
            puts("fstat failed");
            // send 500 to client?
            return 1;
        }

        char http_header[] = "HTTP/1.1 200 OK\r\n\r\n";
        send(client_socket, http_header, sizeof(http_header) - 1, 0);

        for(size_t count = statbuf.st_size; count; count -= n) {
            ssize_t n = sendfile(client_socket, html_data, NULL, count);
            if(n == -1) {
                puts("error");
                break;
            }
        }
        close(html_data);
        shutdown(client_socket, 2);
    }
}

You did not supply a multi-line index.html but using my test file curl localhost:4999 returned:

<html>
        <body>
                <h1>Hello World</h1>
        </body>
</html>
  • Related