nodejs中UDP服务的构建

nodejs中UDP服务的构建

by 伍雪颖

server.js
var dgram = require('dgram');
var server = dgram.createSocket("udp4");

server.on("message",function(msg,rinfo) {
     console.log("Server got: "+msg+" from "+rinfo.address+":"+rinfo.port);
});

server.on("listening",function() {
     var address = server.address();
     console.log("server listening "+ address.address+":"+address.port);
});

client.js
var dgram = require('dgram');
var message = new Buffer("send from client");
var client = dgram.createSocket("udp4");
client.send(message,0,message.length,41234,"localhost",function(err,bytes) {
     client.close();
});


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。