标签:
var net = require(‘net‘);
var server = net.createServer();
server.on(‘connection‘,function(socket){
socket.setEncoding(‘utf8‘);
socket.on(‘data‘,function(data){
console.log(data.toString());
});
socket.on(‘end‘,function(){
console.log(‘end‘);
});
});
server.listen(8765,‘127.0.0.1‘);
var net = require(‘net‘);
var client = new net.Socket();
client.setEncoding(‘utf8‘);
client.connect(8765,‘127.0.0.1‘,function(){
console.log(‘connect‘);
client.write(‘to server‘);
client.end(‘end‘);
});
client.on(‘data‘,function(data){
console.log(‘receive data from server‘);
});
var dgram = require(‘dgram‘);
var server = dgram.createSocket(‘udp4‘);
server.on(‘message‘,function(msg,rinfo){
console.log(msg);
var buf = new Buffer(‘测试‘);
server.send(buf,0,buf.length,rinfo.port,rinfo.address);
});
server.on(‘listening‘,function(){
console.log(‘listen‘);
});
server.bind(12345,‘127.0.0.1‘);
server.on(‘message‘,function(msg,rinfo){
console.log(msg);
var buf = new Buffer(‘测试‘);
server.send(buf,0,buf.length,rinfo.port,rinfo.address);
});
标签:
原文地址:http://www.cnblogs.com/SLchuck/p/4927611.html