首页 > 代码库 > nodejs中UDP服务的构建

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();
});


nodejs中UDP服务的构建