3

我正在学习 Node.js,我正在 udp 上做一些示例代码。我正在尝试实现一个 udp 服务器来收听多播消息,我的基本代码是这样的

var server = require('dgram').createSocket('udp4');
server.on('message', function(message, rinfo) {
console.log('server got message: ' + message + ' from ' + rinfo.address +
':' + rinfo.port);
});
server.bind(4000);
server.addMembership('230.1.2.3');

但是节点给了我一个错误,

events.js:72     
        throw er; // Unhandled 'error' event                     
        Error: bind EINVAL                      
at errnoException (dgram.js:440:11)     
at dgram.js:207:28                                   
at dns.js:72:18                                  
at process._tickCallback (node.js:415:13)            
at Function.Module.runMain (module.js:499:11)        
at startup (node.js:119:16)                          
at node.js:901:3   

我也在使用Windows 7。请帮助...

4

1 回答 1

4

Try

var server = require('dgram').createSocket('udp4');
server.on('message', function(message, rinfo) {
    console.log('server got message: ' + message + ' from ' + rinfo.address +
    ':' + rinfo.port);
});
server.bind(4000,function(){
    server.addMembership('230.1.2.3');
});

On second thought this error may arise due to several reasons like socket is busy, socket is closed, port is in use by some other process etc. so check for those too.

Found a reference after some googleing:https://github.com/joyent/node/issues/4944

于 2013-09-11T06:32:00.223 回答