简体   繁体   中英

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). The device is sending TCP packets to the Nodejs server. 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. 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...

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> .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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