1

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?

3
  • Please show us your code on the client you use to print/inspect the response. :) Commented Feb 5, 2013 at 8:17
  • @rdrey: I did not write any client code. I simply did wget http://localhost:9880 and looked at the file using a hex editor (Okteta in my case) Commented Feb 5, 2013 at 8:30
  • Oops, my bad. I thought this was a Socket.io question. Your wget is a perfectly reasonable way to test this. :) Commented Feb 5, 2013 at 9:34

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.