简体   繁体   English

当数据以缓冲区格式从物联网设备传入时,NodeJS 未收到完整的 tcp 数据包

[英]NodeJS not receiving full tcp packet when data comes in buffer format from iot devices

We have an IOT device for real-time communication (GPS location).我们有一个用于实时通信(GPS 定位)的物联网设备。 The device is sending TCP packets to the Nodejs server.设备正在向 Nodejs 服务器发送 TCP 数据包。 But when we receiving so it is in buffer format and stripped.但是当我们收到时,它是缓冲区格式并被剥离。 We use tcpdump to checking, what data is coming in the network/transport layer.我们使用 tcpdump 来检查网络/传输层有什么数据。 And Data is in HEX format.并且数据是十六进制格式。

0x0000:  029e 280b b191 022c 1666 c6e4 0800 4500  ..(....,.f....E.
0x0010:  0061 0ae6 0000 6706 b735 6b54 5c96 ac1f  .a....g..5kT\...
0x0020:  1d72 5478 0c08 bc9e fa71 4bff c417 5010  .rTx.....qK...P.
0x0030:  03c4 2571 0000 aa55 0035 8305 4572 0051  ..%q...U.5..Er.Q
0x0040:  7601 0101 0209 e55e cde4 eb5e cde4 eb18  v......^...^....
0x0050:  fb48 6dcb c14a 0f00 0040 6c00 0000 1001  .Hm..J...@l.....
0x0060:  4d08 2001 9aff be8f 0d00 000a 0000 00    M..............

But, NodeJS server is receiving data in below format: Buffer aa 55 00 35 83 05 45 72 00 51 76 01 01 01 02 0a d8 5e ce 02 58 5e ce 02 58 18 fb 3e b3 cb c1 4d 3f 00 00 3f e7 00 00 00 10 00 fb 0c 20 01 9a ff bc 8f...但是,NodeJS 服务器正在接收以下格式的数据:缓冲区 aa 55 00 35 83 05 45 72 00 51 76 01 01 01 02 0a d8 5e ce 02 58 5e ce 02 58 18 fb 3e b3 cb c1 4d 3f 00 00 3f e7 00 00 00 10 00 fb 0c 20 01 9a ff bc 8f...

So why we are not able to receive full data packets?那么为什么我们不能接收完整的数据包呢? Because we need full data packets for send acknowledgment purposes.因为我们需要完整的数据包来进行发送确认。

var net = require('net');
net.createServer(function (socket) { 
    socket.on('data', function(data) {
        var text = data.toString('hex');
        console.log("data in hex", text);
    });
    socket.on('end', function() {
        console.log('end'); 
    });
    socket.on('close', function() {
        console.log('close');
    });
    socket.on('error', function(e) {
        console.log('error ', e); 
    }); 

    }).listen(3080, function() { 
        console.log('TCP Server is listening on port 3080'); 
    });
});

Note: Sorry, My English is very poor.注意:对不起,我的英语很差。

I came across the same issue, when Buffer string is too long and you Log it, it appears some like ff... 2 more bytes> .我遇到了同样的问题,当缓冲区字符串太长并且你记录它时,它看起来有点像ff... 2 more bytes>

I managed getting back my entire string by converting the Buffer to Hex.我设法通过将缓冲区转换为十六进制来取回我的整个字符串。

var net = require('net');
net.createServer(function (socket) { 
    socket.on('data', function(data) {
        const hexData = data.toString('hex')
        console.log("data in hex", hexData);
    });
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM