PANIC:调用Lua API时出现未保护的错误(wificonfig.lua:33:使用中的地址) [英] PANIC: unprotected error in call to Lua API (wificonfig.lua:33: address in use)

查看:530
本文介绍了PANIC:调用Lua API时出现未保护的错误(wificonfig.lua:33:使用中的地址)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用frightanic.com的NodeMCU自定义构建在带有lua的ESP8266上创建本地http服务器.

I'm trying to create a local http server on ESP8266 with lua using NodeMCU custom build by frightanic.com.

当我创建一个本地http服务器以及已经在端口80上侦听并从服务器站点获取数据的连接时,这给了我PANIC错误.

When i create a local http server along with a connection that is already listening on port 80 and fetching data from my server site, it is giving me PANIC error.

这是我的代码:

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
wifi.sta.connect()

tmr.alarm(1,10000, 1, function()
    if (wifi.sta.getip() == nil) then
        print("IP unavaiable, Waiting...")
    else
        foo()
        local_server()
    end
end)


function foo()
        conn = nil
        conn=net.createConnection(net.TCP,0)
        conn:on("receive", function(conn, payload)
            print("payload : "..#payload);
        end)
        conn:connect(80,"server.co.in")
        conn:on("connection", function(conn, payload)
            conn:send("GET /mypage?id=deadbeef HTTP/1.0\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)

        conn:on("sent",function(conn)
                print("Closing server connection")
                conn:close()
            end)
end

function local_server()
    srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
        conn:on("receive", function(client,request)
            local buf = "Hello world";
            client:send('HTTP/1.0\n\n')
            client:send('<!DOCTYPE HTML>\n')
            client:send('<html>\n')
            client:send('<head><meta  content="text/html; charset=utf-8">\n')
            client:send('<title>Local server</title></head>\n')
            client:send('<style>body{ background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>\n";')
            client:send(buf);

            conn:on("sent",function(conn)
                srv:close() -- Even tried with client:close()
            end)
        end)
    end)

end

这不可能吗?如果是,那我该怎么办?

Is it not possible to do this ? If yes, than how can i do it ?

如果我在计时器内创建一次服务器,则不会创建任何本地服务器,即.我无法打开本地服务器192.168.x.y.

If i create server once inside timer, than it doesn't create any local server ie. i'm not able to open my local server 192.168.x.y.

这是我修改的代码:

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
wifi.sta.connect()

tmr.alarm(1,10000, 1, function()
    if (wifi.sta.getip() == nil) then
        print("IP unavaiable, Waiting...")
    else
        print("Creating a server");
        srv=net.createServer(net.TCP,0)
        srv:listen(80,function(conn) 
        end)
        tmr.stop(1)
        tmr.alarm(1,10000, 1, function() 
              foo()
              local_server()
        end)
    end
end)


function foo()
        conn = nil
        conn=net.createConnection(net.TCP,0)
        conn:on("receive", function(conn, payload)
            print("payload : "..#payload);
        end)
        conn:connect(80,"server.co.in")
        conn:on("connection", function(conn, payload)
            conn:send("GET /mypage?id=deadbeef HTTP/1.0\r\nHost: server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)

        conn:on("sent",function(conn)
                print("Closing server connection")
                conn:close()
            end)
end

function local_server()
        conn:on("receive", function(client,request)
            local buf = "Hello world";
            client:send('HTTP/1.0\n\n')
            client:send('<!DOCTYPE HTML>\n')
            client:send('<html>\n')
            client:send('<head><meta  content="text/html; charset=utf-8">\n')
            client:send('<title>Local server</title></head>\n')
            client:send('<style>body{ background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>\n";')
            client:send(buf);

            conn:on("sent",function(conn)
                srv:close() -- Even tried with client:close()
            end)
        end)
end

推荐答案

        client:send('HTTP/1.0\n\n')

这不是有效的HTTP响应.投放HTML页面的响应至少应如下所示:

This is not a valid HTTP response. At minimum a response serving a HTML page should look like this

 HTTP/1.0 200 ok\r\n
 Content-type: text/html\r\n
 \r\n
 here comes the HTML body

但这可能不是引起恐慌的原因,因为它抱怨使用的地址.我对ESP8266上运行的操作系统一无所知,但通常该错误意味着该地址和端口已经绑定了一个套接字,并且您无法在同一位置拥有另一个套接字.因此,如果已经有一个Web服务器在该系统上运行,那么您将无法在同一地址启动另一个Web服务器.

But this is probably not the reason for the panic since it complains about address in use. I don't know anything about the OS running on ESP8266 but usually this error means that there is already a socket bound to this address and port and that you cannot have another one at the same location. Thus if there is already a web server running at this system then you cannot start another web server at the same address.

如果成功启动服务器,无法通信(由于服务器的HTTP响应不正确),然后尝试重新启动服务器,然后再次尝试重新启动服务器,也会发生这种错误:重新使用服务器需要一些时间.与该套接字建立连接后,该套接字将变为同一套接字,因此您可能需要等待几分钟,然后才能再次启动服务器(或只是重新引导系统).

Such error can also happen if you've started the server once successfully, failed to communicate (due to improper HTTP response by your server) and then try to restart the server again: it will take some time before you can reuse the same socket once a connection was established to this socket so you might need to wait some minutes before starting a server again (or just reboot the system).

仔细查看您的代码,您可以看到服务器是从计时器内部启动的:

Taking a closer look at your code one can see that the server is started from inside a timer:

tmr.alarm(1,10000, 1, function()
...
        local_server()

快速浏览 tmr的文档似乎您正在使用tmr.ALARM_AUTO调用计时器,因此它将一次又一次地重复.这意味着在您已经设置服务器之后,计时器将重复执行,从而尝试在同一端口上再次设置服务器.这会导致地址使用错误.

From a quick look at the documentation of tmr it looks like you are calling the timer with tmr.ALARM_AUTO so that it will repeat again and again. This means that the timer will be repeated after you've setup the server already and thus try to setup the server again - on the same port. This causes the error of address in use.

这篇关于PANIC:调用Lua API时出现未保护的错误(wificonfig.lua:33:使用中的地址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆