1.创建基本的TCP服务器
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.创建基本的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.创建tcp服务器的一种方式 服务器绑定事件
服务器:
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.获取服务器地址参数
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()方法 用于获取地址参数的操作 返回一个json格式的对象
5.获得最大连接数
服务器
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();
})
多开几个控制台作为客户端试试
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()获取结果一样
服务器
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();
})