英文:
My http webserver written in c doesn't show the html file
问题
I'm building a web server based on the c sockets after I did all what I have the connection is successfully established but not showing the html on the browser
我正在构建一个基于C套接字的Web服务器,经过所有步骤后,连接已成功建立,但浏览器上未显示HTML。
It's just loading endlessly without showing anything
它只是无休止地加载,没有显示任何内容。
For more details you can see my git hub
要获取更多详细信息,您可以查看我的GitHub。
The code
代码
I tried Changing the http massages, I expecting to know where is the bug
我尝试更改HTTP消息,我希望知道错误在哪里。
英文:
I'm building a web server based on the c sockets after I did all what I have the connection is successfully established but not showing the html on the browser
It's just loading endlessly without showing anything
For more details you can see my git hub
https://github.com/mostafashraf18/webserver/tree/main
The code
`
#include "../include/server.h"
#include <stdio.h>
#define BACKLOG 10
int main() {
  WSADATA wsaData;
 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
 fprintf(stderr, "WSAStartup failed.\n");
 exit(1); }
 if (LOBYTE(wsaData.wVersion) != 2 ||
 HIBYTE(wsaData.wVersion) != 2) {
 fprintf(stderr,"Versiion 2.2 of Winsock is not available.\n");
 WSACleanup();
 exit(2);
  }
  
  FILE *html_data;
  html_data = fopen("index.html", "r");
  char response[1024];
  fgets(response, 1024, html_data);
  char http_header[2048] = "HTTP/1.1 200 OK\r\n";
  strcat(http_header, "Content-Type: text/html\r\n");
  strcat(http_header, "\r\n");
  strcat(http_header, response);
  int server_socket;
  server_socket = socket(AF_INET, SOCK_STREAM, 0);
  
  struct sockaddr_in server_address;
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(8001);
  server_address.sin_addr.s_addr = INADDR_ANY;
  
  
  
  bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
  listen(server_socket, BACKLOG);
  int client_socket;
  while(1){
    
    client_socket = accept(server_socket, NULL, NULL);
    send(client_socket, http_header, sizeof(http_header), 0);
    close(client_socket);
  }
  
  
  
  return 0;
  
}
I tried Changing the http massages, I expecting to know where is the bug
答案1
得分: 0
fgets
函数只会读取一行数据,所以它只会读取你的index.html
文件中的以下一行内容:
<!DOCTYPE html>
这解释了为什么你的浏览器只显示一个空白页面。
如果你想将整个文件读入response
中,我建议你要么在循环中调用fgets
,要么使用fread
。如果你使用fread
,那么你将需要手动添加终止空字符,因为fread
不会自动写入它。
我建议你将这行代码:
fgets(response, 1024, html_data);
替换为:
size_t chars_read = fread( response, 1, sizeof response - 1, html_data );
response[chars_read] = ''\0'';
英文:
The function fgets
will only read a single line of data, so it will only read the following line from your index.html
file:
<!DOCTYPE html>
This explains why your browser is only showing an empty page.
If you want to read the entire file into response
, I suggest that you either call fgets
in a loop or use fread
instead. If you use fread
, then you will have to add the terminating null character manually, because fread
will not write one.
I suggest that you replace the line
fgets(response, 1024, html_data);
with:
size_t chars_read = fread( response, 1, sizeof response - 1, html_data );
response[chars_read] = 'size_t chars_read = fread( response, 1, sizeof response - 1, html_data );
response[chars_read] = '\0';
';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论