尝试设置广播标志时出现异常 [英] exception when trying to set broadcast flag

查看:69
本文介绍了尝试设置广播标志时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 dhcp 服务器/客户端上工作,想在我的家庭网络中尝试一些东西.我想通过 udp 将 DHCPDISCOVER 消息发送到我的网络并从我的路由器中获取答案.这是我的代码:

I'm working on a dhcp server/client and wanted to try stuff in my home network. I want to send a DHCPDISCOVER message via udp into my network and catch the answer from my router. Here is my code:

var sender = dgram.createSocket('udp4');
sender.bind();
sender.on('error', function(err) {
  console.log(err);
});

sender.setBroadcast(true);

var listener = dgram.createSocket('udp4');
listener.bind(68, function() {
  console.log('BOUND');
});
listener.on('message', function(msg, rinfo) {
  console.log(msg);
});

sender.send(message, 0, message.length, 67, "255.255.255.255");

但是当我尝试设置广播标志时,我的应用程序崩溃了.

But when I try to set the broadcast flag my application crashes.

sudo node main

dgram.js:345
    throw errnoException(process._errno, 'setBroadcast');
          ^
Error: setBroadcast EBADF
    at errnoException (dgram.js:454:11)
    at Socket.setBroadcast (dgram.js:345:11)
    at Object.<anonymous> (/Volumes/Projects/node-dhcp-server/node_modules/module-dhcp/main.js:31:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
felix@felixs-macbook-pro:/Volumes/Projects/node-dhcp-server/node_modules/module-dhcp%               

我不知道为什么会崩溃...

I have no idea why it crashes...

推荐答案

嗯,好吧,这是一个明显的错误...当然 bind() 方法是异步的,我必须在回调中设置标志.

Hmm ok this was an obvious fault ... Of course the bind() method is asynchronous and I have to set the flag in the callback.

sender.bind( function() { sender.setBroadcast(true) } );

这篇关于尝试设置广播标志时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆