neelsg
neelsg

Reputation: 4842

node.js Buffer binary output is different

This has me very confused. I have the following socket server (simplified) in node.js:

net.createServer(function (socket) {
    socket.on('data', function (data) {
        var replyData = new Buffer('78780E00C425BA53269830303000006C2D0D0A',
            'hex').toString('binary');
        socket.end(replyData);
    });
}).listen(config.port);

What I would expect it to reply to any client is the binary as specified in hexadecimal in the buffer, but it actually replies: 78780E00C38425C2BA5326C298303030006C2D0D0A

This is similar, but not exactly what it should send. What am I missing?

Upvotes: 1

Views: 2040

Answers (1)

Laurent Perrin
Laurent Perrin

Reputation: 14881

It's very simple, just pass a Buffer directly to your socket:

var net = require('net');

net.createServer(function (socket) {
  socket.on('data', function (data) {
    socket.end(new Buffer('78780E00C425BA53269830303000006C2D0D0A', 'hex'));
 });
}).listen(config.port);

EDIT: just reread your question and figured out your problem was not getting the data as binary. Nonetheless, my code works for me:

laurent ~/dev/test $ wget http://localhost:3001 --output-document=data
laurent ~/dev/test $ hexdump data
0000000 78 78 0e 00 c4 25 ba 53 26 98 30 30 30 00 00 6c
0000010 2d 0d 0a                                       
0000013

Tell me if you get something different.

Upvotes: 2

Related Questions