socket accept 和 receive 之间的竞争

我在使用 nodemcu 和 esp-32,最近遇到了一个令人烦恼的问题。我从 NodeMCU Github 页面 中引用了这个示例:

-- 一个简单的 HTTP 服务器
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on("receive", function(sck, payload)
        print(payload)
        sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1> Hello, NodeMCU.</h1>")
    end)
    conn:on("sent", function(sck) sck:close() end)
end)

但这并不是在所有情况下都有效。如果我使用 telnet,就没有问题:

$ telnet 172.17.10.59 80
Trying 172.17.10.59...
Connected to 172.17.10.59.
Escape character is '^]'.
GET / HTTP/1.1
HTTP/1.0 200 OK
Content-Type: text/html

<h1> Hello, NodeMCU.</h1>
Connection closed by foreign host.

但是当使用 wget 时,它大部分时间都会挂起:

$ wget http://172.17.10.59/
--2017-05-12 15:00:09--  http://172.17.10.59/
Connecting to 172.17.10.59:80... connected.
HTTP request sent, awaiting response...

经过一些调查研究,根本原因似乎是在从客户端接收到第一个数据后,receive 回调函数 才被注册,这在使用 telnet 手动测试时并不会发生,但对于像 wget 或浏览器这样的客户端来说,连接和接收第一个数据之间的延迟似乎太小,无法首先注册接收处理程序。

我查看了 nodemcu 代码,没有看到解决这个问题的简单方法。或者我在这里错过了什么?

点赞
用户737647
用户737647

在HTTP/1.0中,当有消息体时,需要在HTTP头部中包含 "Content-Length"。

例如:

"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 25 \r\n\r\n<h1> Hello, NodeMCU.</h1>"

参考:https://www.w3.org/Protocols/HTTP/1.0/spec.html#Content-Length

2021-11-05 08:55:34