我的程序在执行 dgram.udp4 套接字的 send() 方法时抛出 EACCES 错误,但仅在 Windows 上执行。
代码:
var dgram = require('dgram');
var monsocket = dgram.createSocket("udp4");
monsocket.on("listening", function () { comBroadcastCallUp(); });
var comBroadcastCallUp = function() {
var message = new Buffer(JSON.stringify({
protocol: "psdp",
command: "call-up"
}));
monsocket.setBroadcast(true);
monsocket.send(message, 0, message.length, 32681, '255.255.255.255', function (err) {
if (err) console.log(err)
else console.log("<PcStatus:PSDP> Message sent: " + message + os.EOL + "Message length: " + message.length);
});
monsocket.setBroadcast(false);
}
monsocket.bind(32681);
相同的代码,Windows 上的终端输出:
{ [Error: send EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'send' }
Linux 上的终端输出:
<PcStatus:PSDP> Message sent: {"protocol":"psdp","command":"call-up"}
Message length: 39
我检查了防火墙,如果有其他程序使端口繁忙,则什么都没有...
最佳答案
我相信从 Windows 7 开始不再支持广播到 255.255.255.255
。您需要将发送限制到特定子网。
另请记住,并非所有路由器都会路由发往 255.255.255.255
的数据包。
关于windows - 在 Windows 上发送数据报时出现 EACCES 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836651/