1.udp服务器端
var dgram = require('dgram');
var HOST = '127.0.0.1';
var PORT =8877;
var server = dgram.createSocket('udp4');
server.on('listening',function(){
console.log("UDP Server listening on....");
console.info();
});
server.on('message',function(message,remote){
console.log("emmited message event");
console.info();
server.close();
});
server.on('error',function(err){
console.log("server error:\n"+err.stack);
console.info();
server.close();
});
server.on('close',function(){
console.log("server close");
console.info();
});
server.bind(PORT,HOST);
客户端
var dgram = require('dgram');
var HOST = '127.0.0.1';
var PORT =8877;
var message = new Buffer('UDP client to server: hello server');
var client = dgram.createSocket('udp4');
client.send(message,0,message.length,PORT,HOST,function(err,bytes){
if(err){
throw err;
}
console.log("UDP message sent to....");
console.info();
client.close();
});
client.on('close',function(){
console.log('client disconneced');
console.info();
})
2.简单的udp应用
服务器:
var dgram = require('dgram');
var HOST = '127.0.0.1';
var PORT =8877;
var server = dgram.createSocket('udp4');
server.on('listening',function(){
var address = server.address();
console.log("UDP Server listening on"+address.address+":"+address.port);
console.info();
});
server.on('message',function(message,remote){
console.log("received from"+remote.address+':'+remote.port);
console.log("-"+message);
console.info();
server.close();
});
server.on('error',function(err){
console.log("server error:\n"+err.stack);
console.info();
server.close();
});
server.on('close',function(){
console.log("server close");
console.info();
});
server.bind(PORT,HOST);
客户端:
var dgram = require('dgram');
var HOST = '127.0.0.1';
var PORT =8877;
var message = new Buffer('UDP client to server: hello server');
var client = dgram.createSocket('udp4');
client.send(message,0,message.length,PORT,HOST,function(err,bytes){
if(err){
throw err;
}
console.log("UDP message sent to"+HOST+':'+PORT);
console.info();
console.info(bytes);
console.info();
client.close();
});
client.on('close',function(){
console.log('client disconneced');
console.info();
})
3.数据报Udp广播服务的实现
服务器
var dgram = require('dgram');
var HOST = '127.0.0.1';
var PORT =8877;
var server = dgram.createSocket('udp4');
server.on('listening',function(){
var address = server.address();
console.log("UDP Server listening on"+address.address+":"+address.port);
console.info();
});
server.on('message',function(message,remote){
console.log("received from"+remote.address+':'+remote.port);
console.log("-"+message);
console.info();
server.close();
});
server.on('error',function(err){
console.log("server error:\n"+err.stack);
console.info();
server.close();
});
server.on('close',function(){
console.log("server close");
console.info();
});
server.bind(PORT);
客户端
var dgram = require('dgram');
var HOST = '255.255.255.255';
var PORT =8877;
var message = new Buffer('UDP client to server: hello server');
var client = dgram.createSocket('udp4');
client.bind(function(){
client.setBroadcast(true);
});
client.send(message,0,message.length,PORT,HOST,function(err,bytes){
if(err){
throw err;
}
console.log("UDP message sent to"+HOST+':'+PORT);
console.info();
console.info(bytes);
console.info();
client.close();
});
client.on('close',function(){
console.log('client disconneced');
console.info();
})