最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handling Node.js socket data - Stack Overflow

programmeradmin0浏览0评论

I have server receiving data from a client [GPS Device]. I have problem presenting the data (i.e the results obtained from the client) in a readable format. Below are the things I have tried.

Doing:

console.log(data)

I get

<Buffer d0 d7 3d 00 c4 56 7e 81>

Also tried

 console.log(data.toString())

But I get unwanted results:See below:

��A�V~�

Here is my full code:

var net = require('net');
var fs = require('fs');

var server = net.createServer(function (socket) {
  console.log('Server started: Waiting for client connection ...');
  console.log('Client connected:port,address: '+socket.remotePort,      socket.remoteAddress);
  socket.on('data', function (data) {
        var date = new Date();
        var today = date.getDate()+'_'+date.getMonth();
        fs.appendFile(today+'_log.txt', data, function (err) {
          if (err) throw err;
            console.log(data.toString())

    });
 });
});

server.listen(my_port, my_ip);

Thanks for your input.

I have server receiving data from a client [GPS Device]. I have problem presenting the data (i.e the results obtained from the client) in a readable format. Below are the things I have tried.

Doing:

console.log(data)

I get

<Buffer d0 d7 3d 00 c4 56 7e 81>

Also tried

 console.log(data.toString())

But I get unwanted results:See below:

��A�V~�

Here is my full code:

var net = require('net');
var fs = require('fs');

var server = net.createServer(function (socket) {
  console.log('Server started: Waiting for client connection ...');
  console.log('Client connected:port,address: '+socket.remotePort,      socket.remoteAddress);
  socket.on('data', function (data) {
        var date = new Date();
        var today = date.getDate()+'_'+date.getMonth();
        fs.appendFile(today+'_log.txt', data, function (err) {
          if (err) throw err;
            console.log(data.toString())

    });
 });
});

server.listen(my_port, my_ip);

Thanks for your input.

Share Improve this question asked Feb 18, 2014 at 10:48 Cheruiyot FelixCheruiyot Felix 1,5976 gold badges27 silver badges42 bronze badges 7
  • Any idea about the data getting received? Whether it is base64 encoded or ascii? – user1741851 Commented Feb 18, 2014 at 11:04
  • No clear details on encoding used. One of my biggest problem. The protocal doc doesn't show – Cheruiyot Felix Commented Feb 18, 2014 at 11:07
  • Not much help without knowing that. Trial and error might work. Here's a doc that can help. docs.nodejitsu.com/articles/advanced/buffers/how-to-use-buffers – user1741851 Commented Feb 18, 2014 at 11:09
  • I have found somewhere in the vendor website talking about 'ISO-8859-1' encoding standard. Let me find out about it. – Cheruiyot Felix Commented Feb 18, 2014 at 11:19
  • ascii encoding gave me this result - PW=\u0000DV~\u0001 This is the only one that made some sense to me. – user1741851 Commented Feb 18, 2014 at 11:29
 |  Show 2 more comments

3 Answers 3

Reset to default 10

According to the documentation, you must specify an encoding to get a String instead of a Buffer:

Event: 'data'#
Buffer object
Emitted when data is received. The argument data will be a Buffer or String. Encoding of data is set by socket.setEncoding().

You could configure the socket to get the data in UTF-8, for example, with:

socket.setEncoding('utf8');

Do it like this

socket.on('data', function (data) {
        var buff = Buffer.from(data);

But remember, lots of GPS devices use little-endian, so later you're going to have to decode the data as well.

Assuming the data in buffer is 7 bit ASCII,

console.log(data.toString('ascii'))

would resolve the problem.

发布评论

评论列表(0)

  1. 暂无评论