// UDP // 服务端 var dgram = require('dgram'); var server = dgram.createSocket('udp4'); server.on('message', function(msg, rinfo) { console.log('服务端获取信息:'+msg+'来自:'+rinfo.address+':'+rinfo.port); }); c server.on('listening', function() { var address = server.address(); console.log('服务端正在监听:'+address.address+':'+address.port); }); server.bind(41234); // 客户端 var dgram = require('dgram'); var client = dgram.createSocket('udp4'); var message = new Buffer('我是客户端的消息'); client.send(message, 0, message.length, 41234, '127.0.0.1', function(err, bytes) { console.log('客户端发送完成,关闭客户端'); client.close(); });
来自:http://www.cnblogs.com/xiaoheimiaoer/p/3934324.html