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

标签 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

我检查了防火墙,如果有其他程序使端口繁忙,则什么都没有...

最佳答案

我相信从 Windows 7 开始不再支持广播到 255.255.255.255。您需要将发送限制到特定子网。

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

关于windows - 在 Windows 上发送数据报时出现 EACCES 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836651/

相关文章:

windows - 从wmic.exe调用中提取感兴趣的行

C 输入 - getchar()

javascript - 如何查找 npm 重复包?

node.js - 将 Canvas 上播放的视频发送到 websocket

c - 使用 C 的 Windows 进程间通信 (IPC)

c# - 如何获取所有Windows组?

node.js - Socket.IO 和 Firebase 有什么区别?

c++ - 用 C++ 编写简单的类似 FTP 的客户端/服务器应用程序

Java 套接字心跳

c# - 如何获取本地网络上运行我的程序的主机的 IP 地址