在Windows上发送数据报时出现EACCES错误

时间:2014-09-14 18:33:18

标签: windows node.js sockets udp

我的程序在执行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

我检查防火墙,如果其他程序让端口忙,没什么......

1 个答案:

答案 0 :(得分:1)

我认为,从Windows 7开始,不再支持向255.255.255.255广播。您需要将发送限制为特定的子网。

另请注意,并非所有路由器都会路由发往255.255.255.255的数据包。