nodejs中UDP服务的构建
来源:程序员人生 发布时间:2014-12-14 09:05:17 阅读次数:4106次
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();
});
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠