程序员人生 网站导航

nodejs中UDP服务的构建

栏目:综合技术时间:2014-12-14 09:05:17

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


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐