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 <adilhas...@gmail.com> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> 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 nodejs+unsubscr...@googlegroups.com.
> 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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
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 nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to