Hello Fedor,
it took me a bit of fumbling around, but thank you, thank you, thank
you! Just in case someone else comes across a similar problem the
following worked for me:
var msgheader = "message header";
var msg = msgheader + "msg body";
var msghlen = msgheader.length;
var buf = new Buffer(msgheader.length+4);
buf.writeUInt32(msghlen, 0);
buf.write(msg, 4);
var client = net.createConnection({host: 'localhost', port: 8080});
client.write(buf);
client.end();
That worked! The C server was able to pick-up the correct length and
decode the message. I have other problems now, but I think I can sort
those out.
Again thanks!
adil
On Wed, Aug 21, 2013 at 03:34:35PM +0400, Fedor Indutny wrote:
> Hi!
>
> I'd suggest you to try using [Buffer][0] for binary data and send your line
> either in chunks:
>
> socket.write('string');
> socket.write(binary_buffer);
> socket.write('rest');
>
> Or in one buffer (if your server expects data to be in one chunk) by
> writing strings and binary data into one buffer, using `buffer.write` and
> `buffer.writeUInt32`.
>
> [0]: http://nodejs.org/api/buffer.html
>
> Cheers,
> Fedor.
>
>
> On Wed, Aug 21, 2013 at 6:16 AM, Adil Hasan <[email protected]> wrote:
>
> > Hello,
> > I wonder if you can help me. I am new to nodejs and I am trying to write
> > a little application that will talk to a server written in C (the server
> > is third-party). The server requires XML messages with a binary string
> > for the length of the message being sent. The string is meant to
> > represent a unsigned int 32bit word. I'm not so familiar with bit
> > manipulation.
> >
> > I tried the following code (follwing an example on stackexchange):
> >
> > var net = require('net');
> > var result = '';
> > var msgheader = "some string";
> > var msg = "some other string";
> > var msglen = msgheader.length;
> > result += String.fromCharCode(msglen >>> 24 & 0xFF);
> > result += String.fromCharCode(msglen >>> 16 & 0xFF);
> > result += String.fromCharCode(msglen >>> 8 & 0xFF);
> > result += String.fromCharCode(msglen & 0xFF);
> >
> > var smsg = result + msgheader + msg;
> >
> > var client = net.createConnection({host: 'localhost', port: 1250});
> > client.write(smsg);
> > client.end();
> >
> > The C server picks up the correct length if the msglen is 127 or less.
> > But, if it's 128 or more the server doesn't seem to decode the length
> > correctly (it decodes a much larger length) and reads part of the
> > message instead of reading just the header.
> >
> > I'm not sure what the problem is or how to go about fixing it. It almost
> > seems as if the packing of the string is missing something. But, I'm not
> > sure what.
> >
> > The following ruby code works fine.
> >
> > require 'socket';
> > s = TCPSocket.open('localhost', 1250);
> > msg = "header message..."
> > hmsg = hmsg + "some message..."
> > smsg = [hmsg.length].pack("N") + msg
> > s.write(smsg)
> > s.close
> >
> > Does anyone have any ideas or suggestions?
> > Many thanks,
> > adil
> >
> > --
> > --
> > Job Board: http://jobs.nodejs.org/
> > Posting guidelines:
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > You received this message because you are subscribed to the Google
> > Groups "nodejs" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/nodejs?hl=en?hl=en
> >
> > ---
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.