I have recently upgraded my node.js version from 0.10.31 to 4.0.0 by installing n
via npm and then called n stable
.
With the new node version, my existing code broke.
This code:
var d = require("dgram");
var s = d.createSocket("udp4");
s.bind(9000);
var s6 = d.createSocket("udp6");
s6.bind(9000);
produces the following error:
events.js:141
throw er; // Unhandled 'error' event
^
Error: bind EADDRINUSE ::0:9000
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at dgram.js:213:18
at doNTCallback3 (node.js:440:9)
at process._tickCallback (node.js:346:17)
at Function.Module.runMain (module.js:477:11)
at startup (node.js:117:18)
at node.js:951:3
as soon as the IPv6 UDP socket wants to bind to port 9000.
There is no old node process running, and also there is no program already listening on port 9000.
If I change the second bind command to s6.bind(9001);
the error won't occur. If I change the order (udp6 binds first, udp4 second), then the error will show up when the udp4 socket tries to bind.
Could it be that the new node version tries to use the old core modules or something like that?
Can anyone explain this weird behaviour?
Thank you in advance for any help!
Regards
After comparing the source code of the old node version 0.10.31 to 4.0.0, I found out the reason why my code didn't work anymore:
Because node.js does not set the flag IPV6_V6ONLY for IPv6 UDP sockets, one IPv6 socket and one IPv4 socket can only listen to the same port, if SO_REUSEADDR is enabled. The old node version (actually the old version of the libuv library used) set this option implicitly. The new version lets the user choose, but it's disabled by default.
So I had to change my code to the following to get it working:
var d = require("dgram");
var s = d.createSocket({type:"udp4",reuseAddr:true});
s.bind(9000);
var s6 = d.createSocket({type:"udp6",reuseAddr:true});
s6.bind(9000);