通信结束tcp服务器nodejs

时间:2018-04-23 14:45:42

标签: node.js tcp

知道TCP是基于流的协议,在以下nodejs中的客户端服务器示例中,告诉服务器客户端是否已完成所有数据的发送?

服务器

var net = require('net');

var server = net.createServer(function(socket) {

    var remoteAddress = socket.remoteAddress + ':' + socket.remotePort;
  console.log('new client connected: %s', remoteAddress);

    socket.write('Echo server');
    socket.pipe(socket);
});

server.listen(1337, '127.0.0.1');

客户端

var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
    console.log('Connected');

    for ( var i=0 ; i<100; i++) {
      //  console.log(i);
       client.write('Hello, server! Love, Client.\r\n');
    }

});

client.on('data', function(data) {
    console.log('Received: ' + data);
    client.destroy(); // kill client after server's response
});

client.on('close', function() {
    console.log('Connection closed');
});

由于

0 个答案:

没有答案