在网络服务器中显示默认页面的问题
problem with showing the default page in web server
我有一个简单的网络服务器,当我 运行 我的网络服务器并尝试通过 firefox 访问它时,它给我 bin 文件下载而不是显示默认页面 index.html
如果我把它写成 localhost:8001/index.html 它工作正常并且它显示 index.html 文件的内容
我该如何修复它,以便当我像 localhost:8001 这样输入时,它只打开 index.html 文件并显示它的内容?
我的代码的主要功能:
#define PORT 8001
int main()
{
int server_socket, new_socket;
long value;
struct sockaddr_in address;
int addrlen = sizeof(address);
char response_data[1024];
//open a file to serve
FILE *html_data = fopen("/home/seclab/www/index.html","r");
fgets(response_data, 1024, html_data);
char http_header[2048] = "HTTP/1.0 200 OK\r\n\n";
strcat(http_header, response_data);
//create a socket
if((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("can not create socket");
return 0;
}
//define the address
//memset(address.sin_zero, '[=10=]' , sizeof address.sin_zero );
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
address.sin_addr.s_addr = INADDR_ANY;
if(bind(server_socket, (struct sockaddr *) &address, sizeof(address)) < 0 )
{
perror("can't bind the address to the socket");
return 0;
}
if(listen(server_socket, 8) < 0)
{
perror("In listen");
exit(EXIT_FAILURE);
}
while(1)
{
//printf("\nwaiting for a connection\n\n");
if((new_socket = accept(server_socket,NULL , NULL)) < 0)
{
perror("In accept");
exit(EXIT_FAILURE);
}
send(new_socket, http_header, sizeof(http_header), 0);
printf("%s",http_header);
//char buffer[30000] = {0};
//value=read(new_socket, buffer , 30000);
//printf("%s\n",buffer);
//write(new_socket, response_data , strlen(response_data));
//printf("Greeting message sent\n");
close(new_socket);
}
return 0;
}
截图:
您 send
的响应是 sizeof(http_header)
但这应该是响应的实际大小,即 header + 读取的数据:
send(new_socket, http_header, strlen(http_header), 0);
我有一个简单的网络服务器,当我 运行 我的网络服务器并尝试通过 firefox 访问它时,它给我 bin 文件下载而不是显示默认页面 index.html 如果我把它写成 localhost:8001/index.html 它工作正常并且它显示 index.html 文件的内容 我该如何修复它,以便当我像 localhost:8001 这样输入时,它只打开 index.html 文件并显示它的内容?
我的代码的主要功能:
#define PORT 8001
int main()
{
int server_socket, new_socket;
long value;
struct sockaddr_in address;
int addrlen = sizeof(address);
char response_data[1024];
//open a file to serve
FILE *html_data = fopen("/home/seclab/www/index.html","r");
fgets(response_data, 1024, html_data);
char http_header[2048] = "HTTP/1.0 200 OK\r\n\n";
strcat(http_header, response_data);
//create a socket
if((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("can not create socket");
return 0;
}
//define the address
//memset(address.sin_zero, '[=10=]' , sizeof address.sin_zero );
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
address.sin_addr.s_addr = INADDR_ANY;
if(bind(server_socket, (struct sockaddr *) &address, sizeof(address)) < 0 )
{
perror("can't bind the address to the socket");
return 0;
}
if(listen(server_socket, 8) < 0)
{
perror("In listen");
exit(EXIT_FAILURE);
}
while(1)
{
//printf("\nwaiting for a connection\n\n");
if((new_socket = accept(server_socket,NULL , NULL)) < 0)
{
perror("In accept");
exit(EXIT_FAILURE);
}
send(new_socket, http_header, sizeof(http_header), 0);
printf("%s",http_header);
//char buffer[30000] = {0};
//value=read(new_socket, buffer , 30000);
//printf("%s\n",buffer);
//write(new_socket, response_data , strlen(response_data));
//printf("Greeting message sent\n");
close(new_socket);
}
return 0;
}
截图:
您 send
的响应是 sizeof(http_header)
但这应该是响应的实际大小,即 header + 读取的数据:
send(new_socket, http_header, strlen(http_header), 0);