为什么 TCP/IP 服务器响应和客户端数据不一样?

Why the TCP/IP server response and client data is not same?

我每 1 秒发送一个随机生成的 TCP 服务器响应。客户端收到的数据不一样,为什么?

您可能会看到下面的 TCP 服务器和客户端代码及其各自的日志并不相同。两个日志中的 val 字段不同,但 id 和 date 是正确的。谁能告诉我为什么?

到目前为止我尝试了什么

  1. On server.js randomData() --> Math.random() * 100
  2. On server.js randomData() --> parseInt(Math.random() * 100)
  3. On server.js randomData() --> " " + parseInt(Math.random() * 100)
以上的

none 有效。我认为可能是因为

TCP 服务器代码 -

var net = require('net');

// Configuration parameters
var HOST = '127.0.0.1';
var PORT = 1337;
var i = 0;
// Create Server instance 
var server = net.createServer((server) => {
    var remoteAddress = server.remoteAddress + ':' + server.remotePort;
    console.log('new client connected: %s', remoteAddress);
    server.on('data', function() {
        setInterval(() => {
            let response = JSON.stringify(randomData(i));
            console.dir(randomData(i), { depth: null, colors: true })
            server.write(response);
            i++;
        }, 1000);

    });
    server.on('close', function() {
        console.log('connection from %s closed', remoteAddress);
    });
    server.on('error', function(err) {
        console.log('Connection %s error: %s', remoteAddress, err.message);
    });
});

server.listen(PORT, HOST, function() {
    console.log('server listening on %j', server.address());
});

function randomData(id) {
    let data = {
        id: id,
        val: " " + parseInt(Math.random() * 100) + " ",
        date: Date()
    }
    return data;
}

服务器日志 -

node server.js
server listening on {"address":"127.0.0.1","family":"IPv4","port":1337}
new client connected: 127.0.0.1:62227
{ id: 0,
  val: ' 96 ',
  date: 'Mon Mar 19 2018 01:12:16 GMT+0530 (India Standard Time)' }
{ id: 1,
  val: ' 70 ',
  date: 'Mon Mar 19 2018 01:12:17 GMT+0530 (India Standard Time)' }
{ id: 2,
  val: ' 70 ',
  date: 'Mon Mar 19 2018 01:12:18 GMT+0530 (India Standard Time)' }
{ id: 3,
  val: ' 74 ',
  date: 'Mon Mar 19 2018 01:12:19 GMT+0530 (India Standard Time)' }
{ id: 4,
  val: ' 34 ',
  date: 'Mon Mar 19 2018 01:12:20 GMT+0530 (India Standard Time)' }
{ id: 5,
  val: ' 1 ',
  date: 'Mon Mar 19 2018 01:12:21 GMT+0530 (India Standard Time)' }
{ id: 6,
  val: ' 86 ',
  date: 'Mon Mar 19 2018 01:12:22 GMT+0530 (India Standard Time)' }
{ id: 7,
  val: ' 6 ',
  date: 'Mon Mar 19 2018 01:12:23 GMT+0530 (India Standard Time)' }

TCP 客户端代码 -

let net = require('net');
let config = {
    'data': 'mr black',
    'format': 'JSON'
}

let client = new net.Socket();

client.connect(1337, '127.0.0.1', function() {
    console.log('Connected');
    client.write(JSON.stringify(config));
});

client.on('data', function(data) {
   /* console.log(data);
    console.log(JSON.parse(data.toString()));*/
    console.dir(JSON.parse(data.toString()), { depth: null, colors: true })
});

client.on('error', function(err) {
    console.log(err);
    client.connect(1337, '127.0.0.1', function() {
        console.log('Connected');
        client.write(JSON.stringify(config));
    });
});

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

客户端日志 -

Connected
{ id: 0,
  val: ' 95 ',
  date: 'Mon Mar 19 2018 01:12:16 GMT+0530 (India Standard Time)' }
{ id: 1,
  val: ' 77 ',
  date: 'Mon Mar 19 2018 01:12:17 GMT+0530 (India Standard Time)' }
{ id: 2,
  val: ' 67 ',
  date: 'Mon Mar 19 2018 01:12:18 GMT+0530 (India Standard Time)' }
{ id: 3,
  val: ' 81 ',
  date: 'Mon Mar 19 2018 01:12:19 GMT+0530 (India Standard Time)' }
{ id: 4,
  val: ' 45 ',
  date: 'Mon Mar 19 2018 01:12:20 GMT+0530 (India Standard Time)' }
{ id: 5,
  val: ' 2 ',
  date: 'Mon Mar 19 2018 01:12:21 GMT+0530 (India Standard Time)' }
{ id: 6,
  val: ' 41 ',
  date: 'Mon Mar 19 2018 01:12:22 GMT+0530 (India Standard Time)' }
{ id: 7,
  val: ' 14 ',
  date: 'Mon Mar 19 2018 01:12:23 GMT+0530 (India Standard Time)' }

在 server.js 中,当您两次调用 randomData 时,您会生成两个不同的响应 - 一个您发送,另一个您记录。

let response = JSON.stringify(randomData(i));
console.dir(randomData(i), { depth: null, colors: true })

您应该将对象 response 传递给您的 console.dir 调用,而不是调用 randomData 两次(这将生成两个单独的随机数)。