Question

I wonder if anyone can help me. I'm new to nodejs and I've been trying to send a message to a server using nodejs as the client. The server is written in C and looking at a PHP installation it makes use of the pack('N',len) to send the length of the string to the server. I have tried to implement a similar thing in javascript but am hitting some problems. I wonder if you can point out where I am going wrong (credit to phpjs from where I copied the packing string code).

My client nodejs javascript code is:

   var net = require('net');
     var strs = Array("<test1>1</test1>",` 
        "<test_2>A STRING</test_2>", "<test_3>0</test_3>", 
        "<test_4></test_4>", "<test_5></test_5>",
        "<test_6></test_6>", "<test_7_></test_7>",
        "<test_8></test_8>", "<test_9>10</test_9>",
        "<test_10></test_10>", "<test_11></test11>",
        "<test_12></test_12>", "<test_13></test_13>",
        "<test_14></test_14>");

        hmsg = strs[0] + strs[1] + strs[2] + strs[3] + strs[4] + strs[5] + strs[6];
        console.log(hmsg.length);
        msg =  hmsg + "<test_20></test_20>"`

     msglen = hmsg.length;
     astr = '';
     astr += String.fromCharCode((msglen >>> 24) && 0xFF);
     astr += String.fromCharCode((msglen >>> 16) && 0xFF);
     astr += String.fromCharCode((msglen >>> 8) & 0xFF);
     astr += String.fromCharCode((msglen >>> 0) & 0xFF);
     var pmsg = astr + msg;
     console.log(pmsg);
     var client = net.createConnection({host: 'localhost', port: 1250});
     console.log("client connected");
     client.write(pmsg);
     client.end();

Running 'node testApp' prints out the correct length of the header string. If I look at what the server is receiving I can see that as long as the header string is < 110 chars it decodes the correct length, but if the header string is > 110 (by adding strs[6] or more to the hmsg) the decoded length is incorrect. Including strs[6] I get a string of length 128 on the client side and 194 on the server side.

I am clearly doing something wrong in packing the integer, but I'm not familiar with packing bits and am not sure where I am going wrong. Can anyone point out where my error is? Many thanks!

Update Thanks to Fedor Indutny on the nodejs mailing list the following worked for me:

console.log(hmsg.length, msg.length);
var msglen = hmsg.length;
var buf = new Buffer(msg.length+4);
mslen = buf.writeUInt32BE(msglen, 0);
mslen = buf.write(msg, 4);

var client = net.createConnection({host: 'localhost', port: 8190});
console.log("client connected");
client.write(buf);
client.end();

I.e. using the Buffer's writeUInt32 was all that was needed for the length of the header message. I'm posting here in the hope it may help someone else.

Was it helpful?

Solution

I see that you managed to get it working on your own, but for a much easier fix you could just use the binary module.

https://github.com/substack/node-binary

Install with npm install binary

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top