PANIC: unprotected error in call to Lua API (wificonfig.lua:33: address in use)

China☆狼群 提交于 2019-12-12 01:52:21

问题


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

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.

Here's my code :

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 ?

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.

Here's my modified code :

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

回答1:


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

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

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.

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()

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.



来源:https://stackoverflow.com/questions/43819786/panic-unprotected-error-in-call-to-lua-api-wificonfig-lua33-address-in-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!