Skip to content

Critical issue: no apparent way to use pure binary TCP sockets in Node 6.9.5 #11316

@RickBullotta

Description

@RickBullotta

Version 6.9.5
Windows 10
Net and Stream subsystem issue

I am trying to use Node to read (and write) binary streams from a TCP socket. I have tried every possible approach including:

socketClient.setEncoding(null)
socketClient.setEncoding('binary')

setEncoding(null) results in UTF-8 encoding being applied and screws everything up. setEncoding('binary') still seems to be modifying the bytes from the incoming stream in some cases.

I have verified the correct/actual inbound byte values with a Java app, so the issue is confined to Node.

Here is a concrete example:

The following 10 bytes (hex) are sent from the server:

00 02 00 00 00 04 00 c8 00 01

...however, when I try to read them from the Node stream associated with the socket, I see the following:

00 02 00 00 00 04 00 c3 88 00 01

Note that there are now 11 bytes, and c8 has been replaced with c3 88.

I have spent a couple days trying to figure this out, and have had zero luck. Is this a bug with the Stream classes? Is there some magic encoding setting that I don't know about?

Please advise. Thanks!

**** UPDATE ****

I may have found a workaround. The issue actually seems to be the conversion of the "data" parameter in the 'data' event emitted by the stream/socket.

I was doing:

client.on('data', function(data) {
         var rawBuffer = Buffer.from(data);

...and that was not working. Out of desperation, I tried:

client.on('data', function(data) {
         var rawBuffer = Buffer.from(data,'binary');

...and it worked.

If you'll indulge my venting (after losing many hours on this), the documentation for use of encoding on buffers and streams SUCKS. Not only is there no listing of the various options and their behaviors, but no usage information whatsoever.

This needs to be addressed to prevent others from experience the three days of hell I just went through...

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions