Search code examples
node.jssocketsudpremote-accesslan

bind udp server to external ip via port forwarding


Is it possible to connect a udp socket to an external ip after port forwarding is done?

var PORT = 33333;
var HOST = 'xxx.xx.xxx.xxx'; // my external ip

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

server.on('listening', function () {
    var address = server.address();
    console.log('UDP Server listening on ' + address.address + ":" + 
address.port);
});

server.on('message', function (message, remote) {
    console.log(remote.address + ':' + remote.port +' - ' + 
message);

});

server.bind(PORT, HOST);

port forwarding already set up (below)

any source forwards to 127.0.0.1:33333

I'm getting 'EADDRNOTAVAIL' error

Error: bind EADDRNOTAVAIL xxx.xx.xxx.xxx:33333

Is this even possible? and if not, what are my options for listening in to remote ip addresses?

Thank Asaf


Solution

  • You can't bind to a non-local address, and you don't need to. Just bind to 0.0.0.0. The port forwarding will do the rest.