使用 poll() 的 HTTP 持久连接

问题描述 投票:0回答:1

成功创建侦听套接字后,我尝试在其上运行一段应保持持久连接的代码块。这是我到目前为止所做的:

while(1) {
  struct pollfd pfds[1];
  pfds[0].fd = sockfd; // sockfd is my connection socket that I work on
  pfds[0].events = POLLIN;
  int num_events = poll(pfds, 1, 2000); // timeout 2 seconds
  if (num_events == 0) {
    printf("No data to read\n");
    close(sockfd);
    break;
  } else {
    // work with sockfd, recv() data from it and at the very end, do not close the connection
  }
}

我正在尝试用curl 测试上面的代码。我运行

curl hostname:portnum hostname:portnum
(连续两次)并得到以下行为:curl 立即获取我在 else 块内渲染的 html 页面并等待 2 秒,然后发送另一个请求。期望的行为是在发送第一个请求后立即发送另一个请求。

curl -v localhost:4390 localhost:4390
*   Trying 127.0.0.1:4390...
* Connected to localhost (127.0.0.1) port 4390 (#0)
> GET / HTTP/1.1
> Host: localhost:4390
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Keep-Alive: timeout=2, max=100
* no chunk, no close, no size. Assume close to signal end
< 
<html>
  Hello, world!
</html>
* Closing connection 0
* Hostname localhost was found in DNS cache
*   Trying 127.0.0.1:4390...
* Connected to localhost (127.0.0.1) port 4390 (#1)
> GET / HTTP/1.1
> Host: localhost:4390
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Keep-Alive: timeout=2, max=100
* no chunk, no close, no size. Assume close to signal end
< 
<html>
  Hello, world!
</html>
* Closing connection 1

以上是我现在从

curl
得到的。期望的行为是这样的。

curl -v google.com google.com
 *   Trying 142.250.203.206:80...
 * Connected to google.com (142.250.203.206) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.81.0
> Accept: */*
> 
 * Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-V5HivFgCR5mSmZ20PuojOg' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
< Date: Sun, 07 Jul 2024 08:54:11 GMT
< Expires: Tue, 06 Aug 2024 08:54:11 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
 * Connection #0 to host google.com left intact
 * Found bundle for host google.com: 0x5edef1e05010 [serially]
 * Can not multiplex, even if we wanted to!
 * Re-using existing connection! (#0) with host google.com
 * Connected to google.com (142.250.203.206) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.81.0
> Accept: */*
> 
 * Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce-Junna8vFZULg3bC31jZ9NA' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
< Date: Sun, 07 Jul 2024 08:54:11 GMT
< Expires: Tue, 06 Aug 2024 08:54:11 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
 * Connection #0 to host google.com left intact

如您所见,托管 google.com 的连接 #0 保持不变。

c linux http sockets tcp
1个回答
0
投票

事实证明,您必须添加“Content-Length”,以便客户端在获取应获取的每个数据字节后关闭连接。

© www.soinside.com 2019 - 2024. All rights reserved.