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);
效果: