[英]Why does my html file only load if it's all on one line? [C Socket Programming]
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:我最近在 C 开始学习套接字编程,在网上学习了一个教程之后,我最终得到了一个服务器代码,它加载了一个可以在浏览器中查看的 html 文件:
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.我第一次尝试使用浏览器 (Brave Browser) 连接到此服务器时,我能够连接,但我只能看到一个空白页面。 I later found out that the only way my html would load is if it were all on one line, like it is here:
后来我发现我的 html 加载的唯一方式是如果它都在一行上,就像它在这里:
<!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.
我也在 Firefox 上尝试过这个,以确保这不仅仅是铬的东西。 Can someone enlighten me?
有人可以启发我吗? Thank you.
谢谢你。
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.考虑至少为开发设置套接字选项
SO_REUSEADDR
,这样您就不必等待 tcp/ip 超时就可以再次启动服务器。
Here is an example using sendfile()
.这是一个使用
sendfile()
的例子。 read()
use a similar calling pattern. read()
使用类似的调用模式。
#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:您没有提供多行 index.html 但使用我的测试文件 curl localhost:4999 返回:
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.