Page 1 of 1
Why won't this very simple UDP networking work?
Posted: Wed Nov 13, 2019 7:56 pm
by pauls313
This is a very very simple attempt at networking with unconnected UDP objects, but I don't understand why it won't work.
Client = sends information
Server = receives information
Press t to change type
Re: Why won't this very simple UDP networking work?
Posted: Wed Nov 13, 2019 11:15 pm
by pgimeno
The semantics of not using setsockname() on the server are not clear to me, as there's no port defined. The LuaSocket manual says:
"To associate an UDP socket with a local address, an application calls the setsockname method before sending any datagrams. Otherwise, the socket is automatically bound to an ephemeral address before the first data transmission and once bound the local address cannot be changed".
However, I could not make it work until I used setsockname first.
Edit: If it works according to the udp(7) manpage, it seems it's listening on some automatically assigned port:
udp(7) manpage wrote:
In
order to receive packets, the socket can be bound to a local address
first by using bind(2). Otherwise, the socket layer will automatically
assign a free local port out of the range defined by
/proc/sys/net/ipv4/ip_local_port_range and bind the socket to
INADDR_ANY.
Edit2: The docs for LuaSocket's setsockname say this:
"Note: This method can only be called before any datagram is sent through the UDP object, and only once. Otherwise, the system automatically binds the object to all local interfaces and chooses an ephemeral port as soon as the first datagram is sent. After the local address is set, either automatically by the system or explicitly by setsockname, it cannot be changed".