Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:03:32+00:00 2026-05-23T20:03:32+00:00

I need to send a JSON string to a number of TCP clients from

  • 0

I need to send a JSON string to a number of TCP clients from a node.js TCP server.

In order to read the messages from the socket/stream on the client side I need to do some sort of message framing. One way to do this is to prefix the message with the length of the message as an array – then convert that to the buffer size for the message on the client side.

How would I do something like this in node.js/javascript on the server and then read it out on the client side using a .NET client?

Given this client side code, how would I frame the message correctly on the server side using javascript/node?

TcpClient client = new TcpClient(server, port);
var netStream = client.GetStream();

// read the length of the message from the first 4 bytes
byte[] b = new byte[4];
netStream.Read(b, 0, b.Length);
int messageLength = BitConverter.ToInt32(b, 0);

// knowing the length, read the rest of the message
byte[] buffer = new byte[messageLength];
netStream.Read(buffer, b.Length, buffer.Length);
var message = System.Text.Encoding.UTF8.GetString(buffer);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:03:33+00:00Added an answer on May 23, 2026 at 8:03 pm

    to unframe incoming data in nodejs you can try to use node-bufferlist or node-buffers
    or create your own FSM manually and feed it with incoming chunks of data

    server side is simplier:

    function sendPacket(stream, buffer)
    {
        var prefix = new Buffer(4);
        var length = buffer.length;
        var offset = 0;
        // serialize 32bit little endian unsigned int
        prefix[offset++] = length & 0xff;
        prefix[offset++] = (length >> 8)  & 0xff );
        prefix[offset++] = (length >> 16)  & 0xff );
        prefix[offset++] = (length >> 24)  & 0xff );
        stream.write(prefix);
        stream.write(buffer);
    }
    

    or you can use node v0.5+ buffer.writeUInt32

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to send a JSON string from the server side to the
Ok i need to use json on my iOS application to send/receive data to/from
I need send certain attributes(say, human readable user name) from server to client after
I need to send some data from my Android device to my server. I
Possible Duplicate: JSON order mixed up By Microsoft convension i need send Json as
I need to use a GET request to send JSON to my server via
I have a JSON string in my javascript, I need to send this to
In on_message() method of SockJS-Tornado library, I want to send JSON messages to clients:
I'm trying to send json string in the get request to the server, here
We need send h264 from flash to Wowza and after to vlc by RTSP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.