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 6020069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:30:52+00:00 2026-05-23T03:30:52+00:00

I have a strange problem. I am getting a stream of text from a

  • 0

I have a strange problem. I am getting a stream of text from a tcp client and writing it to a file. The stream is not fully filled hence while converting it to string the unfilled parts of the byte array are converted to \0 so i finally end up having,

str = "blah foo bar \0\0\0\0\0...";

so what i did is

str = str.trim('\0');

But if i do this then the string is not getting written to a file using stream writer. If i comment the trim line then its getting written along with all the white space characters. Here is my full code

StreamWriter sw = new StreamWriter("c:\\a\\ta.txt");
while (true)
{
    try
    {
        NetworkStream ns = tc.GetStream();
        byte[] instream = new byte[tc.ReceiveBufferSize];
        Thread.Sleep(2500);
        ns.Read(instream, 0, tc.ReceiveBufferSize);
        string decodedData = string.Empty;
        decodedData = System.Text.Encoding.ASCII.GetString(instream);
        decodedData = decodedData.Trim('\0');
        //string a = "dfdsfdsfdsfdsf";
        //string b = a.Trim('\0');

        try
        {
            sw.Write(decodedData);
            //MessageBox.Show(decodedData);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Can some one explain me why this is and how i can solve htis out.

oh on debugging i can see that decodedData has the trimmed text neat and clean but i dont know why its not being written to the file.

  • 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-23T03:30:53+00:00Added an answer on May 23, 2026 at 3:30 am

    There are three problems here.

    First, you grab the text from the whole array, regardless of how many bytes you actually did receive. Most likely this is the source of your zero characters.

    To fix that, change the code as follows:

    int actuallyRead = ns.Read(instream, 0, tc.ReceiveBufferSize);
    string decodedData = Encoding.ASCII.GetString(instream, 0, actuallyRead);
    

    Secondly, you need to close the stream in order for it to flush its contents. The best way to do that is to wrap it in a using block:

    using (StreamWriter sw = new StreamWriter("c:\\a\\ta.txt"))
    {
        ... rest of your code here
    }
    

    Thirdly, the code would normally never complete. Add a way for it to complete without relying on exception handling, for instance:

    int actuallyRead = ns.Read(instream, 0, tc.ReceiveBufferSize);
    if (actuallyRead == 0)
        break;
    string decodedData = Encoding.ASCII.GetString(instream, 0, actuallyRead);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem: I am getting the html source from url using
i have a strange problem... I'm getting string-data from db: $siteDesc = strval( $configHandle->getConfigValue(
I have a strange problem that I could not solve. When I try to
I have a strange problem with reg expression. I'm trying to getting out the
I have quite a strange problem , While copying one WidgetCollection from one FlowPanel
i have one AJAX function getting results from php file as bellow $.ajax({ type:
My problem is about putting and getting variables into/from the play scope. It's not
I have a strange problem with my script. I am getting a JSON result
i have a very strange problem, well not really a problem because i've fixed
I have strange problem with sharepoint and ajax functionality. We have an UpdatePanel placed

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.