简体   繁体   中英

Sending and receiving a UDP broadcast to and from 255.255.255.255 in Node.js

I'm trying to implement a BOOTP server in Node, for which broadcasting is a necessity. Sadly the docs are a little bit confusing and I'm getting weird errors all the way. Funny enough, the errors are different on Windows 7 and Ubuntu.

Did someone actually manage to send a UDP broadcast to 255.255.255.255 or receive one under this address?

Could someone provide me a simple Node UDP broadcasting demo?

Using punt I tried to bind a connection to 255.255.255.255 on port 5000 and I get this error EADDRNOTAVAIL

I think the address it too general. See this link

Here is the code, which is just a slightly modified version of a punt example.

var punt = require('punt');
var server = punt.bind('255.255.255.255:5000');
var a = punt.connect('255.255.255.255:5000');

server.on('message', function(msg){
  console.log(msg);
});

setInterval(function(){
  a.send({ hello: 'world' });
}, 150);

which yields this error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: bind EADDRNOTAVAIL
    at errnoException (dgram.js:439:11)
    at dgram.js:206:28
    at dns.js:72:18
    at process._tickCallback (node.js:415:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)
    at node.js:901:3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM