Sending binary over a socket

4,820 views
Skip to first unread message

shimonchayim

unread,
May 17, 2011, 7:25:42 PM5/17/11
to nodejs
I am trying send binary + Text to an event server over TCP. Sample
message is like this:

0x00 0x00 0x00 0x01 0x00 0x00 0x01 0x00 0xd2 [Text Message 210 char
long]
(4 bytes counter) (4 bytes message len -> 210 in this case)

Here is the problem, if we use encoding as ASCII with net.socket then
things like 0x00 get converted to 0x20 (space) and if I use UTF8 then
0xd2 shows up something like 0xc3 0x92.

How can I send this event in one socket.write message (i.e. avoid
changing the encoding)?

If there is a different module that can also be used, I am open.

Tim Caswell

unread,
May 18, 2011, 12:10:18 AM5/18/11
to nod...@googlegroups.com
Since you know the size of the message, you can create a new buffer object with the total size of the message and set the first 8 bytes manually and then write the string into the buffer at the offset, then send the buffer to the socket without an encoding.



--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Stefan Zehe

unread,
May 18, 2011, 2:29:13 AM5/18/11
to nod...@googlegroups.com
I use
https://github.com/substack/node-binary for reading binary and
https://github.com/substack/node-put for creating binary-data

Works pretty well!

Am 18.05.2011 06:10, schrieb Tim Caswell:
> Since you know the size of the message, you can create a new buffer
> object with the total size of the message and set the first 8 bytes
> manually and then write the string into the buffer at the offset, then
> send the buffer to the socket without an encoding.
>
>
> On Tue, May 17, 2011 at 4:25 PM, shimonchayim <shimon...@gmail.com
> <mailto:shimon...@gmail.com>> wrote:
>
> I am trying send binary + Text to an event server over TCP. Sample
> message is like this:
>
> 0x00 0x00 0x00 0x01 0x00 0x00 0x01 0x00 0xd2 [Text Message 210 char
> long]
> (4 bytes counter) (4 bytes message len -> 210 in this case)
>
> Here is the problem, if we use encoding as ASCII with net.socket then
> things like 0x00 get converted to 0x20 (space) and if I use UTF8 then
> 0xd2 shows up something like 0xc3 0x92.
>
> How can I send this event in one socket.write message (i.e. avoid
> changing the encoding)?
>
> If there is a different module that can also be used, I am open.
>
> --
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com

> <mailto:nod...@googlegroups.com>.


> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com

> <mailto:nodejs%2Bunsu...@googlegroups.com>.


> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.


Mit freundlichen Gr��en

--
Stefan Zehe
0951 - 510 908 106
------------------------------
upjers GmbH & Co. KG
Memmelsdorfer Stra�e 250
96052 Bamberg

Sitz der Gesellschaft: Bamberg
Registergericht: Bamberg, HRA 10694
Gesellschafter: Upjers Verwaltungs GmbH

http://www.upjers.com
------------------------------

shimonchayim

unread,
May 18, 2011, 3:13:40 AM5/18/11
to nodejs
Thanks for the suggestion. This looks like it may work. Some follow-up
question if you don't mind.

May be this is what I was missing, I thought for net.socket we can
only provide string as input, can I also provide a buffer object as
input? What does it do, does it internally do toString()?

Thanks again

On May 17, 9:10 pm, Tim Caswell <t...@creationix.com> wrote:
> Since you know the size of the message, you can create a new buffer object
> with the total size of the message and set the first 8 bytes manually and
> then write the string into the buffer at the offset, then send the buffer to
> the socket without an encoding.
>

shimonchayim

unread,
May 18, 2011, 3:18:39 AM5/18/11
to nodejs
Thanks for the link.
Really nice, will give that a shot.


On May 17, 11:29 pm, Stefan Zehe <s.z...@upjers.com> wrote:
> I usehttps://github.com/substack/node-binaryfor reading binary andhttps://github.com/substack/node-putfor creating binary-data
>
> Works pretty well!
>
> Am 18.05.2011 06:10, schrieb Tim Caswell:
>
>
>
>
>
>
>
>
>
> > Since you know the size of the message, you can create a new buffer
> > object with the total size of the message and set the first 8 bytes
> > manually and then write the string into the buffer at the offset, then
> > send the buffer to the socket without an encoding.
>

Laurie Harper

unread,
May 18, 2011, 3:32:39 AM5/18/11
to nod...@googlegroups.com
Other way around: if you pass Socket.write() a string, the 'encoding' argument specifies what encoding to use to convert it to bytes. If you pass it a Buffer, it just writes the bytes in the buffer.

L.

--
Laurie Harper
http://laurie.holoweb.net/

shimonchayim

unread,
May 18, 2011, 12:40:34 PM5/18/11
to nodejs
Appreciate the help, just to make sure, I understand.

If I do net.Socket.write(<String>) then it will take the default
encoding=utf8 but if I pass net.Socket.write(<Binary Buffer>) then no
encoding will be assumed. Is that right?

- Cylus

mscdex

unread,
May 18, 2011, 4:19:38 PM5/18/11
to nodejs
On May 18, 12:40 pm, shimonchayim <shimoncha...@gmail.com> wrote:
> Appreciate the help, just to make sure, I understand.
>
> If I do net.Socket.write(<String>) then it will take the default
> encoding=utf8 but if I pass net.Socket.write(<Binary Buffer>) then no
> encoding will be assumed. Is that right?

Correct, a Buffer is already bytes so there is nothing to encode.
String need to be converted to bytes somehow and the encoding
specifies how to do that.

You can also specify other encodings in .write() if it's not utf8
text, for example: socket.write(myBase64Str, 'base64') or
socket.write(myUCS2Str, 'ucs2') etc.

shimonchayim

unread,
May 18, 2011, 4:31:22 PM5/18/11
to nodejs
Makes sense. Thanks all.

FYI, great experience with my first post. You guys are awesome.
Reply all
Reply to author
Forward
0 new messages