Connection Refused when using UDP receive

230 views
Skip to first unread message

Jilin Xie

unread,
May 17, 2016, 3:04:37 AM5/17/16
to openre...@googlegroups.com
I'm hoping to make Nginx receive udp packets and then do some operations with the data by lua.
I start with this config (test-1):
             https://github.com/openresty/stream-lua-nginx-module/blob/master/t/087-udp-socket.t

The connect and send phase worked, but with receive, it kept getting:

              recv() failed (61: Connection refused)


I can't find much information about udp usage in nginx (http server).
So, can someone have a look what I'm doing wrong?

http {

    resolver 127.0.0.1 ipv6=off;

    include       mime.types;

    server {

        default_type text/html;

        listen 8081;

        server_tokens off;

        location / {

            resolver 8.8.8.8;

            content_by_lua '

                local udp = ngx.socket.udp()

                udp:settimeout(1000)


                local ok, err = udp:setpeername("127.0.0.1", 8081)

                if not ok then

                    ngx.say("failed to connect: ", err)

                    return

                end

                ngx.say("connected")

                local req = "\\0\\1\\0\\0\\0\\1\\0\\0flush_all\\r\\n"

                local ok, err = udp:send(req)

                if not ok then

                    ngx.say("failed to send: ", err)

                    return

                end

                ngx.say("sent")


                local data, err = udp:receive()

                if not data then

                    ngx.say("failed to receive data: ", err)

                    return

                end

                ngx.print("received ", #data, " bytes: ", data)

            ';

      }

    }

}



Jilin Xie

unread,
May 17, 2016, 3:41:45 AM5/17/16
to openresty-en
Ah, sorry. I just realized I misunderstood the test config from agentzh. 
There should be another running udp server at  (  local ok, err = udp:setpeername("127.0.0.1", port))
What I got wrong is, I thought udp:setpeername has 'registered' a udp server inside nginx......

So, I'll need to have a stream server to handle the udp package. Sorry for the noise.
Reply all
Reply to author
Forward
0 new messages