Only a part of my data is read (TCP, NodeJS)

First time asking a question here (Stackoverflow is getting too toxic for me) and hopefully someone will be able to help me.

I am working on a communication program meant to transmit large chunks of data. However, it fails at exactly that - large chunks of data. I narrowed it down to exactly 1538 characters being transmitted before someone going wrong.

I am under the impression that NodeJS reads the data before it finished transmitted.

Code (simplified):

socket.on('data', function(data) {
        data = data.toString(); //This is 1538 characters
        plain = decrypt(data).toString(); //This is 724 characters. It is JSON in text format
        json = JSON.parse(plain); //This throws error

       /* SyntaxError: Unexpected end of input
          JSON is *obviously invalid as the last curly bracket is missing (as well as extra data)
       */

Hopefully someone will manage to help me out ^^

1 Like

Hi Sattoshi_Vox welcome to the forum

Are you sure you need decrypt and that stringify wouldn’t be the better to use?

Yes? I am?

And want to transform an encrypted string to JSON. Not sure what you are talking about.

Plus it’s an issue with TCP transmissions and not json itself.

plain is json in plain text form.

I’m not sure if I can help, but just to be clear data = data.toString(); data is 1538 characters here and plain = decrypt(data).toString(); plain is being truncated here?

If that’s the case, it sounds like something is going on in the decrypt, not the connection.

And are you sure the data being transmitted in a normal string and not a JSON string? What about data = JSON.parse(data) instead of .toString()? Or if it is being transmitted in JSON, it could be something as simple as decrypt(data.fieldToBeDecrypted) instead of the other stuff.

Also, what libraries are you using for the socket connection and decrypt functions?

1 Like

The reason I asked about decrypt is because that looks like where the problem is happening

data = data.toString(); //This is 1538 characters
plain = decrypt(data).toString(); //This is 724 characters

That is, it’s starting with 1538 characters and ending with only 724

Does decrypt have an option to specify the packet size?

EDIT
mawburn ninja’d :smile:

1 Like

The encryption algorithm increases the total amount of characters. The limit is hit with 1538 characters BEFORE being decrypted. Encryption algo is fine.

I am now doing a terribly hacky solution. I am appending all data received to itself until one of the pseudo-packets ends with a dollar sign. The chances that it ends in a dollar sign are very slim so I go with that. (Terrible practice, I know, i’m sorry)

However, it IS working. This does prove that the crypto works fine.

CC: @mawburn

Encryption is done with AES and socket connection with the standard net library.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.