简单c++;web服务器发送错误的标题

简单c++;web服务器发送错误的标题,c,websocket,webserver,C,Websocket,Webserver,我有一个非常简单的web服务器: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #include <

我有一个非常简单的web服务器:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>

char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: Keep-Alive\r\n"
"Server: michal\r\n"
"Vary: Accept-Encoding\r\n"
"Keep-Alive: timeout=5, max=100\r\n\r\n"
"<html><body><h1>It works!</h1>"
"<p>This is the default web page for this server.</p>"
"<p>The web server software is running but no content has been added, yet.</p>"
"</body></html>\r\n";

int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = sizeof(cli_addr);

  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    err(1, "can't open socket");

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

  int port = 8080;
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(port);

  if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
    close(sock);
    err(1, "Can't bind");
  }

  listen(sock, 5);
  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
    printf("got connection\n");

    if (client_fd == -1) {
      perror("Can't accept");
      continue;
    }

    write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
    close(client_fd);
  }
}
当我对本地主机(Apache)进行基准测试时,ab工作正常

当我尝试下载包含php文件内容的页面时,出现以下错误:

PHP Notice:  file_get_contents(): send of 2 bytes failed with errno=32 Broken pipe in /home/mitch/Dokumenty/projects/cpp/webserver/webserver/bench.php on line 7

怎么了?

我猜您是在客户端有机会发送它想要的所有请求头之前终止了连接


因此,在
关闭之前增加延迟,或者在收到空行之前实际读取数据将有所帮助。

这里解释了类似的问题:(p.2)

头是可以的,但您应该先从客户端读取一些内容,然后再写入响应,下面是代码片段的差异,添加了用于读取数据的代码:

@@ -8,6 +8,8 @@
 #include <arpa/inet.h>
 #include <err.h>

+#define MAXMSG  16384
+
 char response[] = "HTTP/1.1 200 OK\r\n"
 "Content-Type: text/html; charset=UTF-8\r\n"
 "Connection: Keep-Alive\r\n"
@@ -24,6 +26,8 @@ int main()
   int one = 1, client_fd;
   struct sockaddr_in svr_addr, cli_addr;
   socklen_t sin_len = sizeof(cli_addr);
+  char read_buffer[MAXMSG];
+  int nbytes;

   int sock = socket(AF_INET, SOCK_STREAM, 0);
   if (sock < 0)
@@ -50,6 +54,13 @@ int main()
       perror("Can't accept");
       continue;
     }
+    nbytes = read (client_fd, read_buffer, MAXMSG);
+
+    if (nbytes < 0) {
+      perror("Can't read");
+      close(client_fd);
+      continue;
+    }

     write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
     close(client_fd);
@-8,6+8,8@@
#包括
#包括
+#定义MAXMSG 16384
+
字符响应[]=“HTTP/1.1 200正常\r\n”
“内容类型:text/html;字符集=UTF-8\r\n”
“连接:保持活动\r\n”
@@-24,6+26,8@@int main()
int one=1,客户端\u fd;
svr\u addr、cli\u addr中的结构sockaddr\u;
socklen\u t sin\u len=sizeof(cli\u addr);
+字符读取缓冲区[MAXMSG];
+整数字节;
intsock=socket(AF_INET,sock_STREAM,0);
if(sock<0)
@@-50,6+54,13@@int main()
佩罗尔(“不能接受”);
持续
}
+nbytes=读取(客户端\u fd、读取\u缓冲区、MAXMSG);
+
+如果(n字节<0){
+佩罗(“不识字”);
+关闭(客户_fd);
+继续;
+    }
写入(客户机fd,响应,sizeof(响应)-1)/*-1:'\0'*/
关闭(客户_fd);

当我添加睡眠(1)时,页面在浏览器中显示1秒,我有一个错误,连接被拒绝(在浏览器中)@MichałK。尝试发送
连接:关闭
而不是
连接:保持活动状态
@@ -8,6 +8,8 @@
 #include <arpa/inet.h>
 #include <err.h>

+#define MAXMSG  16384
+
 char response[] = "HTTP/1.1 200 OK\r\n"
 "Content-Type: text/html; charset=UTF-8\r\n"
 "Connection: Keep-Alive\r\n"
@@ -24,6 +26,8 @@ int main()
   int one = 1, client_fd;
   struct sockaddr_in svr_addr, cli_addr;
   socklen_t sin_len = sizeof(cli_addr);
+  char read_buffer[MAXMSG];
+  int nbytes;

   int sock = socket(AF_INET, SOCK_STREAM, 0);
   if (sock < 0)
@@ -50,6 +54,13 @@ int main()
       perror("Can't accept");
       continue;
     }
+    nbytes = read (client_fd, read_buffer, MAXMSG);
+
+    if (nbytes < 0) {
+      perror("Can't read");
+      close(client_fd);
+      continue;
+    }

     write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
     close(client_fd);