net

2019-10-20  本文已影响0人  招风小妖怪

server

const net = require('net');

var server = net.createServer(function(socket) {
    console.log('client connected');
    socket.on('end', function() {
        console.log('客户端关闭连接');
    });
    socket.on("data",function (data) {
        console.log(data.toString())
    })
    // socket.write('Hello World!\r\n');
    // socket.pipe(socket);
});

client

var net = require('net');
var client = net.connect({port: 8080}, function() {
   console.log('连接到服务器!');  
});
client.on('data', function(data) {
   console.log(data.toString());
   client.end();
});
client.on('end', function() { 
   console.log('断开与服务器的连接');
});

上一篇 下一篇

猜你喜欢

热点阅读