nodejs新建tcpserver
2022/05/10    
var timesCount = 0;
const net = require('net');
const server = net.createServer();

server.on('listening',data=>{
  console.log('listening');
})
server.on('connection',socket=>{
  console.log('client connected');
  socket.on('end', () => {
    console.log('client disconnected');
  });
  socket.on('error',data=>{
    console.log(data);
  })
  for(let i=0;i<1000;i++){
        timesCount+=i;
        setTimeout(function(){
          try{
            socket.write('1111');                      
        },100*i);
    }
});

server.on('error', (err) => {
  console.log(err);
  //throw err;
});
server.listen(4001, () => {
  //console.log('server bound');
});

参考链接 https://www.jianshu.com/p/5864a1d5db8b