1.創(chuàng)建基本的TCP服務(wù)器
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9696;
net.createServer(function(sock){
console.log('Server at '+HOST+':'+PORT);
sock.on('data',function(data){
console.log('socket on data');
});
sock.on('close',function(data){
console.log('socket on close');
});
}).listen(PORT,HOST);
net.createServer([options][,connectionListener]);
2.創(chuàng)建基本的TCP客戶端
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 9696;
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();
});
3.創(chuàng)建tcp服務(wù)器的一種方式 服務(wù)器綁定事件
服務(wù)器:
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var server = net.createServer();
server.on('listening',function(){
console.log("listen at "+PORT);
console.info();
});
server.on('connection',function(socket){
console.log('server has a new connection');
console.info();
server.close();
});
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 =8877;
var client = net.connect(PORT,HOST,function(){
console.log('client connected');
console.info();
client.end();
});
client.on('end',function(){
console.log('client disconnectd');
console.info();
})
4.獲取服務(wù)器地址參數(shù)
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var server = net.createServer();
server.on('listening',function(){
console.log("listen at "+PORT);
console.info();
var addr = server.address();
console.info("open 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.close();
});
server.on('close',function(){
console.log('Server is now closed');
console.info();
});
server.listen(PORT,HOST);
server.address()方法 用于獲取地址參數(shù)的操作 返回一個(gè)json格式的對象
5.獲得最大連接數(shù)
服務(wù)器
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var server = net.createServer();
server.on('listening',function(){
console.log("listen at "+PORT);
console.info();
});
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);
getConnections的方法獲取
客戶端
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var client = net.connect(PORT,HOST,function(){
console.log('clientA connected');
console.info();
client.write("Hello server !");
});
client.on('end',function(){
console.log('clientA disconnectd');
console.info();
})
多開幾個(gè)控制臺作為客戶端試試
6.獲取套接字地址
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var server = net.createServer();
server.on('listening',function(){
console.log("listen at "+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(localPort);
console.info();
server.close();
});
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);
socket.address()與server.address()獲取結(jié)果一樣
7.套接字寫數(shù)據(jù)
服務(wù)器
var net = require('net');
var HOST = '127.0.0.1';
var PORT =8877;
var server = net.createServer();
server.on('listening',function(){
console.log("listen at "+PORT);
console.info();
});
server.on('connection',function(socket){
console.log('server has a new connection');
console.info();
socket.on('data',function(data){
console.info('read:'+socket.bytesRead);
console.info('DATA'+socket.remoteAdress+':'+data+'');
console.info();
socket.write('Server write '+data);
console.info('writen is '+socket.byteWritten);
console.info();
});
server.close();
});
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 =8877;
var sWriteContent = 'hello server!';
var client = net.connect(PORT,HOST,function(){
console.log('clientA connected');
console.info();
client.write(sWriteContent);
});
client.on('data',function(data){
console.log(data.toString());
console.info();
console.info("socket.byteRead is "+client.byteRead);
console.info();
client.end();
})
client.on('end',function(){
console.log('clientA disconnectd');
console.info();
})