我正在用C编写一个简单的HTTP服务器。我已经设法绑定,监听和接受连接。我正在尝试发送一个简单的404:
#define sends(socket, s) send(socket, (s), strlen((s)), 0)
void send_not_found(int socket)
{
sends(socket, "HTTP/1.0 404 Not Found\r\n");
sends(socket, "Content-Type: text/html\r\n");
sends(socket, "Server: brandonisawesome\r\n");
sends(socket, "\r\nnot found");
}
void handle_request(void* param)
{
int sock = *((int*)param);
send_not_found(sock);
close(sock);
}
这导致firefox给我The connection to the server was reset while the page was loading.
Firebug告诉我请求已发送:
实际上,在firefox告诉我连接被重置之前,“未找到”会显示四分之一秒。我做错了什么?
答案 0 :(得分:1)
您错过了Content-Length:
,而Content-Type:
也是个好主意。
对于“未找到”,我认为Content-Length: 9
是正确的。