Sam Sam - 3 months ago 31
Node.js Question

Node.js DGRAM module: Cannot send UDP message to remote machine but can to local machine

I am running a simple UDP server using Nodejs's dgram module.
Relevant code is simply:

server = dgram.createSocket('udp4');
server.bind(10022,'127.0.0.1');


When I netstat on the local machine, using this:

sudo netstat -l


I see this entry:

Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 localhost:10022 *:*


When I do a nmap from a remote machine using the IP address of the interface:

sudo nmap -sU -p 10022 192.168.7.171


I see this:

Nmap scan report for 192.168.7.171
Host is up (0.00032s latency).
PORT STATE SERVICE
10022/udp closed unknown


I have UDP client code running on the same machine which works fine when I use server.bind(10022,'127.0.0.1'), as done above. It does not when I use the real IP address assigned to the interface.

I also want to use the client code from a remote machine. So, when I use server.bind(10022, '192.168.7.171') in place of server.bind(10022,'127.0.0.1'), I would have expected to be able to send messages to the UDP process from a remote machine. I cannot. Interestingly the send function's callback, in the remote machine, does not return an error either.

How do I get the client code on the remote machine send message to the UDP server. All firewalls were shutdown for testing because I initially suspected it to be a firewall issue. In any event, both the machines are on a local intranet during testing.

Answer

The address parameter of bind() is optional, you can try to listen to all local addresses (0.0.0.0) by omitting it but still specify the port. Eg:

server.bind(10022);

Regarding the missing error UDP is by default a not reliable protocol, the transmission will be treated as successful when it's sent on the wire. UDP does not send back and acknowledgement packet, if you need a reliable channel you can use TCP.

Comments