使用net模块创建tcp服务器

demo

/**
 * Created by ZXW on 2018/4/6.
 */
var net=require('net');

const RORT=8080;
const HOST='localhost';

var clientHandler=function(socket) {
  console.log('连接上了');
     socket.on('data',function dataHandle(data) {
        console.log(socket.remoteAddress,socket.remotePort,'send',data);
     });
     socket.on('close',function() {
        console.log(socket.remoteAddress,socket.remotePort,'disconnected');
     });

};
var app=net.createServer(clientHandler);
app.listen(RORT,HOST);
console.log('tcp连接在 tcp://',HOST,':',RORT);

效果:

    

posted @ 2018-04-06 16:48  1点  阅读(260)  评论(0编辑  收藏  举报