Node.js, dgram.setBroadcast(flag) fails due to "EBADF"

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

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick

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.

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)

First it fails in dgram.js on line 352, dgram.js is a internal node.js module abstracting the "low level" code. Line 352 is 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 sockets setBroadcast method.

Going up one more layer, we end up at your hello.js file on line 25 with the client.setBroadcast(true); call.

The rest is more node.js code resulting from the initial load of the hello.js file.

The actual Error

The error thrown by the native code which node.js wraps here is EBADF looking this up in conjunction with UDP gives us:

EBADF
    The socket argument is not a valid file descriptor. 

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:

/*
* Set broadcast on or off
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* on 1 for on, 0 for off
*
* Returns:
* 0 on success, -1 on error.
*/

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.


The method setBroadcast should be called on 'listening' event or passed as callback in bind method:

var socket = dgram.createSocket('udp4');

socket.on('listening', function(){
    socket.setBroadcast(true);
});

socket.bind(8000);

OR:

var socket = dgram.createSocket('udp4');

socket.bind(8000, undefined, function() {
    socket.setBroadcast(true);
});

It seems like the file descriptor is created only on bind or on send, and it's required before setBroadcast. You can call client.bind() with no parameter to bind to a random port before setting broadcast. Don't worry about using a random port, since it's done "lazily" when using client.send anyway.

var sys = require('util');
var net = require('net');

var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.bind();
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
    client.close();
});

Tags:

Node.Js