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

  • SEARCH
  • Home
  • 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 946137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:52:00+00:00 2026-05-15T22:52:00+00:00

From the internet I got the way to read a huge string from a

  • 0

From the internet I got the way to read a huge string from a NetworkStream.

 static NetworkStream ns = null;
 static StringBuilder sb = null;
 static byte[] buffer = null;
 static int position = 0;
 //.......................................
 //other codes skipped for simplicity
 //....................................... 
 private static string Read()
 {
        if (ns.CanRead)
        {
            sb.Clear();
            position = 0;
            while (ns.DataAvailable)
            {
                position = ns.Read(buffer, 0, buffer.Length);
                sb.Append(Encoding.Unicode.GetString(buffer, 0, position));
            }

            return sb.ToString().Trim();
        }
        else
        {
            return null;
        }
 }

However, I cannot find an example how to write a huge string to a NetworkStream.

Is there a “symmetrical” pattern for writing as we do for reading?

Thank you in advance.

  • 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-15T22:52:01+00:00Added an answer on May 15, 2026 at 10:52 pm

    That reading code is dangerously wrong in many ways:

    • By using static variables in this way, it’s hopelessly unsuitable for multi-threaded tasks. (I hope that’s just due to you simplifying it…)
    • It never initializes the variables to non-null values – again, hopefully that’s not the real code
    • It uses the DataAvailable property to decide when it should be “done” – that’s incredibly dangerous as it means if a packet is delayed in the stream, you could read half as much data as you expected to
    • It uses Encoding.Unicode always, which is rarely the best choice of encoding
    • It assumes that it will always read a whole number of characters. What if one character is split between reads? That’s what the Encoder/Decoder classes are for… but you don’t really need to use them here anyway – see below.

    I would strongly suggest that you wrap the NetworkStream in a StreamReader for reading, and a StreamWriter for writing. That’s what they’re for. You can then read a line at a time, or just a char[] buffer, or to the end of the stream (which means “until the socket is closed”). This is fine for a text-only protocol.

    If you’ve got a protocol which mixes text and binary data, life becomes a lot harder. Personally I like protocols which length-prefix messages – that way you can read only the data you’re meant to, and then perform whatever conversion you want.

    Anyway, I hope this random selection of thoughts helps… if you want more detailed assistance, please provide details of what protocol you’re using.

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

Sidebar

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.