猿问

Node.js 组播问题

按照node.js权威指南这本书敲的代码:
vardgram=require('dgram');
varserver=dgram.createSocket('udp4');
server.on('listening',function(){
server.setMulticastTTL(128);
server.addMembership('230.185.192.108');
});
setInterval(broadCast,1000);
functionbroadCast(){
varbuf=newBuffer((newDate()).toLocaleString());
server.send(buf,buf.length,8000,'230.185.192.108');
}
为毛报这样的错啊?我的port是8000,肯定小于65536啊
dgram.js:339
thrownewRangeError('Portshouldbe>0and<65536');
^
RangeError:Portshouldbe>0and<65536
atSocket.send(dgram.js:339:11)
atTimeout.broadCast[as_onTimeout](C:\Users\quan.sun\Desktop\js\node\demo1.js:12:12)
atontimeout(timers.js:386:14)
attryOnTimeout(timers.js:250:5)
atTimer.listOnTimeout(timers.js:214:5)
临摹微笑
浏览 328回答 2
2回答

哔哔one

犯了一个低级错误,应该是server.send(buf,**0**,buf.length,8000,'230.185.192.108');

波斯汪

buf的偏移量没写,一般为0,参考api:socket.send(msg,[offset,length,]port[,address][,callback])如果不想写偏移量,那么参数中也不能有buf长度
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答