I tried to write a simple http server using socket programming in C. I want to first try that the server could send back the HTML file without considering what kind of request is been received.
Here's the header and the body :
char httpHeader[100000] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html>\r\n"
"<html><head><title>Testing</title></head>\r\n"
"<body><p>Testing</p></body><html>\r\n";
Here's the procedure of sending package:
while(1) {
if ( (connfd = accept(listenfd, NULL, NULL) ) < 0 ) {
fprintf(stderr, "accept error\n");
exit(EXIT_FAILURE);
}
if (send(connfd, httpHeader, sizeof(httpHeader), 0) == -1) {
fprintf(stderr, "send error\n");
exit(EXIT_FAILURE);
};
if ( close(connfd) < 0 ) {
fprintf(stderr, "close error\n");
exit(EXIT_FAILURE);
}
}
I bind the socket on port 8080, and using telnet to test whether it's working or not. After running the server and send request by telnet in the same machine, the client side successfully receive the whole package without any error.
But when I try to connect the server using Chrome, it failed. The GET error : "net::ERR_CONNECTION_RESET 200 (OK)" showed up. When I check the network session in DevTool, I saw that the response header is received by Chrome, showing "HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8", but did not see the HTML file.
Is the header format wrong? I check the protocol again but not sure where I do wrong.