【问题标题】:Race between socket accept and receive套接字接受和接收之间的竞争
【发布时间】:2025-12-10 10:15:01
【问题描述】:

我正在使用带有 esp-32 的 nodemcu,最近遇到了一个烦人的问题。我参考了NodeMCU Github page中的这个示例:

-- a simple HTTP server
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 代码,似乎没有一种简单的方法可以解决这个问题。还是我错过了什么?

【问题讨论】:

  • 使用httpserver Lua模块时问题是否依然存在?

标签: lua esp8266 nodemcu


【解决方案1】:

在 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

【讨论】: