没有任何流量监听UPNP

时间:2017-04-08 11:50:41

标签: node.js tcp udp upnp

我有一些NodeJS代码可以监听为UPNP发送的广播消息。一个代码片段在端口1900上侦听,另一部分代码在端口1900上使用dgram代表UDP。两段代码都不返回任何消息。我使用实用程序验证了网络上正在播放的流量。我不会同时运行这两段代码,这样我就避免了任何端口问题。使用netstat -a我可以在代码运行时看到正在使用的端口。

为什么我没有收到这些UPNP消息?

//DServer for UDP
/*dserver = dgram.createSocket('udp4');
dserver.bind('1900', () => {
    console.log('Listening on address:' + dserver.address());
});
dserver.on('error', (err) => {
    console.log('UDP Server ERROR:' + err);
    dserver.close();
});
dserver.on('close', () => {
    console.log('UDP Server closed');
});
dserver.on('listening', () => {
    console.log('UDP Server listening');
});
dserver.on('message', (msg, rinfo) => {
    console.log(msg + '\n' + rinfo);
});*/
//TCP Server ( for testing )
let dserver = net.createServer((socket) => {
    socket.on('data', (data) => {
        console.log("DATA:" + data.toString());
    }).on('error', (err) => {
        console.trace('DServer socket.on: error');
        console.error(err.stack);
    });
}).on('listening', () => {
    console.log('DServer Listening');
}).on('connection', () => {
    console.log('DServer got a connection');
}).on('close', () => {
    //stays open if client closes conx
    console.log('DServer Closed');
}).on('error', (err) => {
    console.trace('DServer.on: error');
    console.error(err.stack);
});
dserver.listen({ port: 1900 }, () => {
    console.log('DServer Started Listening');
});

1 个答案:

答案 0 :(得分:0)

玩了一些dgram方法,这看起来效果很好:

let dgram = require('dgram');

//DServer for UDP
dserver = dgram.createSocket('udp4');
dserver.bind('1900');//UDP Port all UPNP messages broadcast on
dserver.on('error', (err) => {
    console.log('UDP Server ERROR:' + err);
    dserver.close();
});
dserver.on('close', () => {
    console.log('UDP Server closed');
});
dserver.on('listening', () => {//server start to listen after dserver.bind
    console.log('UDP Server listening');
    dserver.setMulticastLoopback(true);//NEEDED to allow broadcast packets
    //on the network interface, NodeJS documentation has more info.
    dserver.setMulticastTTL(128);//This is for sending not needed for recieving
    dserver.addMembership('239.255.255.250');//add to broadcast-IP UPNP range

});
dserver.on('message', (msg, rinfo) => {//When receive message print to screen
    console.log(msg + '\n' + rinfo);
});