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

时间:2018-03-18 19:55:50

标签: node.js tcp server client tcp-ip

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

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

到目前为止我一直在尝试

  1. 在server.js上随机数据() - > Math.random()* 100
  2. 在server.js上随机数据() - > parseInt(Math.random()* 100)
  3. 在server.js上随机数据() - > " " + parseInt(Math.random()* 100)
  4. 以上都没有奏效。我以为可能是因为

    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)' }
    

1 个答案:

答案 0 :(得分:0)

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

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

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