I’m using Node.js 0.6.9, and am trying to send a datagram broadcast package. Code:
var sys = require('util');
var net = require('net');
var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
client.close();
});
Running the code:
$ node test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: setBroadcast EBADF
at errnoException (dgram.js:352:11)
at Socket.setBroadcast (dgram.js:227:11)
at Object.<anonymous> (/home/letharion/tmp/collision/hello.js:25:8)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:41)
Some googling reveals that “EBADF” means “The socket argument is not a valid file descriptor”. But I don’t understand enough about the problem for that to be helpful.
First of all, you seem to have trouble understanding the format of the stacktrace, so let’s clarify it before we go to the actual error that is thrown here.
Format of a node.js Stacktrace
This part is just the location where the internal NodeJS logic choked up and put out the error below:
The actual error stacktrace follows, it shows the deepest location in the callstack first, so going down in the stack trace, brings you up in the call hierachy, eventually leading you to the point in your code where everything began.
First it fails in
dgram.js on line 352,dgram.jsis a internal node.js module abstracting the “low level” code. Line352is in a function containing generic logic for throwing errors.It was called at
dgram.js in line 227, after a failed if check which wraps the call to the wrapped native UDP socketssetBroadcastmethod.Going up one more layer, we end up at your
hello.jsfile on line25with theclient.setBroadcast(true);call.The rest is more node.js code resulting from the initial load of the
hello.jsfile.The actual Error
The error thrown by the native code which node.js wraps here is
EBADFlooking this up in conjunction withUDPgives us:By going further down into the node.js rabbit hole, we end up in the udp wrapper, which wraps the uv wrapper for the actual C implementation, in the uv wrapper we find:
Leading us to the conclusion that your socket has not been initialized yet.
In the end, binding the socket via
client.bind(8000)fixed the missing initialization and made the program run.