TCP/UDP網路應用
建立基本的TPC伺服器 /* var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info(); net.createServer(function(sock){ console.log('Server listening on' + HOST + ':' + PORT); console.info();
sock.on('data' , function(data) {
console.log('socket on data...');
});
sock.on('close' , function(data){
console.log('socket on close...');
});
}).listen(PORT ,HOST);
建立基本的TCP用戶端
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
var client = net.connect(PORT , HOST , function(){ console.log('client connected...'); console.info();
});
client.on('data',function(data){ console.info('client on data...'); console.info(); });
client.on('end',function(){ console.log('client disconnected'); console.info();
});
建立簡單的var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
net.createServer(function(sock){ console.log('Server listening on' + HOST + ':' + PORT); console.info();
console.log('CONNECTED' + sock.remoteAddress + ':' + sock.remotePort);
console.info();
sock.on('data',function(data){
console.info('DATA' + sock.remoteAddress + ':"' + data + '"' );
console.info();
sock.write('Server write : "' + data + '"');
});
sock.on('close',function(data){
console.log('CLOSED:' + sock.remoteAddress + ' '+ sock.remotePort);
console.info();
});
}).listen(PORT,HOST);
客戶端:
var net = require("net"); var HOST = '127.0.0.1'; var PORT = 8888;
var client = net.connect(PORT,HOST,function(){ console.log('client connected'); console.info(); client.write('client write : Hello');
});
client.on('data',function(data){ console.log(data.toString()); console.info(); client.end(); });
client.on('end',function(){ console.log('client disconnected'); console.info();
});
建立另一種TCP伺服器
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
var server = net.createServer();
server.listen(PORT , HOST); console.log('Server listening on' + server.address()); console.info();
server.on('connection',function(sock){ console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort); });
取得伺服器位址參數
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info(); var addr = server.address(); console.info("opened server on"); console.info("%j",addr); console.info(); server.close(); });
server.on('connection' , function(socket){ console.log('Server has a new connection'); console.info(); });
server.on('close', function(){ console.log('Server is now closed'); console.info(); });
server.listen(PORT,HOST);
取得現在伺服器連接數
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info(); / var addr = server.address(); console.info("opened server on"); console.info("%j",addr); console.info(); server.close(); / });
server.on('connection' , function(socket){ console.log('Server has a new connection'); console.info();
server.getConnections(function(err , count){
if(err){
console.info(err.message);
}else{
console.info("current connection is" + count);
console.info();
}
});
});
server.on('close', function(){ console.log('Server is now closed'); console.info(); });
server.on('error',function(err){ console.log('Error occurred:', err.message); console.info(); });
server.listen(PORT,HOST);
取得通訊端位址
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info();
});
server.on('connection' , function(socket){ console.log('Server has a new connection'); console.info(); console.info(socket.address()); console.info(); console.info(socket.localAddress); console.info(); console.info(socket.localPort); server.close();
});
server.on('close', function(){ console.log('Server is now closed'); console.info(); //server.close(); });
server.on('error',function(err){ console.log('Error occurred:', err.message); console.info(); });
server.listen(PORT,HOST);
取得遠程位址
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info();
});
server.on('connection' , function(socket){ console.log('Server has a new connection'); console.info(); console.info(socket.remoteAddress); console.info(); console.info(socket.remotePort); console.info(); server.close();
});
server.on('close', function(){ console.log('Server is now closed'); console.info(); //server.close(); });
server.on('error',function(err){ console.log('Error occurred:', err.message); console.info(); });
server.listen(PORT,HOST);
使用通訊端寫資料
伺服器端
/jslint node:true / 'use strict';
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888;
console.info('Now create Server...'); console.info();
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info();
});
server.on('connection', function(socket){ console.log('Server has a new connection'); console.info();
//為socket實例新增一個data事件處理函數
socket.on('data',function(data){
//列印輸出由用戶端發來的資料位元組長度
console.info('DATA' + socket.bytesRead);
console.info();
//列印輸出由用戶端發來的資料
console.info('DATA' + socket.remoteAddress + ':"' + data + '"');
console.info();
//回發資料給用戶端
socket.write('Server write : "' + data + '"');
console.info();
//列印輸出回發到用戶端的資料位元組長度
console.info('socket.bytesWritten is ' + socket.bytesWritten);
console.info();
});
server.close();
});
server.on('close', function(){ console.log('Server is now closed'); console.info(); //server.close(); });
server.listen(PORT,HOST);
客戶端
/jslint node:true / 'use strict';
var net = require("net"); var HOST = '127.0.0.1'; var PORT = 8888; var swrited = "hahaha";
//使用net.connect()方法建立一個TPC用戶端實例
var client = net.connect(PORT, HOST, function () { console.log('clientA connected'); console.info(); client.write(swrited);
});
client.on('data',function(data){ console.log(data.toString()); console.info(); console.info(client.bytesRead); console.info(); client.end(); });
client.on('end',function(){ console.log('client disconnected'); console.info();
});
控制通訊端資料流程的應用
伺服器端 /jslint node:true / 'use strict';
var net = require('net'); var HOST = '127.0.0.1'; var PORT = 8888; var bSockData = true;
//建立TCP伺服器 var server = net.createServer();
//監聽listening事件
server.on('listening', function(){ console.log('Server is listening on port',PORT); console.info();
}); //監聽connection事件 server.on('connection', function(socket){ console.log('Server has a new connection'); console.info();
if(bSockData){
socket.resume();
bSockData = false;
}else{
socket.pause();
bSockData = true;
}
//為socket實例新增一個data事件處理函數
socket.on('data',function(data){
//列印輸出由用戶端發來的訊息
if(socket.bytesRead > 32){
console.info('DATA' + socket.remoteAddress + ':"'+ "is too long!" + '"');
console.info();
}else{
console.info('DATA' + socket.remoteAddress + ':"' + data + '"');
console.info();
}
//列印輸出由用戶端發來的資料
/* console.info('DATA' + socket.remoteAddress + ':"' + data + '"');
console.info();
*/
//回發資料給用戶端
socket.write('Server write : "' + data + '"');
console.info();
//列印輸出回發到用戶端的資料位元組長度
/* console.info('socket.bytesWritten is ' + socket.bytesWritten);
console.info();*/
});
//server.close();
});
server.on('close', function(){ console.log('Server is now closed'); console.info(); //server.close(); });
server.listen(PORT,HOST);
客戶端
/jslint node:true / 'use strict';
var net = require("net"); var HOST = '127.0.0.1'; var PORT = 8888; var swrited = "hahaha";
//使用net.connect()方法建立一個TPC用戶端實例
var client = net.connect(PORT, HOST, function () { console.log('clientA connected'); console.info(); client.write(swrited);
});
client.on('data',function(data){ console.log(data.toString()); console.info(); console.info(client.bytesRead); console.info(); client.end(); });
client.on('end',function(){ console.log('client disconnected'); console.info();
});
建立簡單的UDP應用
伺服器端
/jslint node:true / 'use strict';
var dgram = require('dgram'); var HOST = '127.0.0.1'; var PORT = 12345;
console.info('create UDP server');
var server = dgram.createSocket('udp4');
server.on('listening', function(){ var address = server.address(); console.info('UDP server listening on...' +address.address + ':' + address.port); //console.log('UDP server listening on...',PORT); console.info(); });
server.on('message', function(message, remote){ console.info('UDP server received from' + remote.address + ':' + remote.port); console.info(); console.log("-" + message); 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 closed'); console.info();
});
//為udp伺服器鎖定主機和阜
server.bind(PORT , HOST);
客戶端
/jslint node:true / 'use strict';
var dgram = require('dgram'); var HOST = '127.0.0.1'; var PORT = 12345;
var message = new Buffer('UDP Client to Server : Hello Server!');
console.info('Now create UDO Client...'); console.info();
var client = dgram.createSocket('udp4');
client.send(message , 0 , message.length , PORT , HOST , function(err,bytes){ if(err){ throw err; } console.log('UDP message send to ' + HOST +':' + PORT); console.info(); console.info(bytes);
client.close();
});
client.on('close', function(){ console.log('client disconnected'); console.info(); });
*/