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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:56:54+00:00 2026-05-31T12:56:54+00:00

I am writing what is essentially an image backup server to store images. It

  • 0

I am writing what is essentially an image backup server to store images. It is a one way service that will not return anything beyond a basic success or failure message to the client.

The issue that I am experienceing is that when I send a byte array through the network stream, it is being cut-off before the end of the stream at random locations. I do not have this issue when I run the server on my development machine and connect locally, but rather it only occurs when the server is deployed on a remote server.

When I send very small arrays ( < 512 bytes) the server recieves the entire stream successfully, but on streams larger than 2000 bytes I experience issues. The code for the client is as follows:

    try
    {
        TcpClient Voice = new System.Net.Sockets.TcpClient();
        //Obviously I use the remote IP when it is deployed - but have altered it for privacy.
        IPEndPoint BackupServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 57000);

        Voice.Connect(BackupServer);
        NetworkStream DataStream = Voice.GetStream();

        byte[] buffer = new ASCIIEncoding().GetBytes(ImageData.GetXml());
        DataStream.Write(buffer, 0, buffer.Length);
        DataStream.Flush();
      }
      catch 
      {
      }
      try
      {
        buffer = new byte[4096];
        int read = DataStream.Read(buffer, 0, buffer.Length);
        MessageBox.Show(new ASCIIEncoding().GetString(buffer) + " : " + read.ToString());
      }
      catch
      {

      }

The client code executes without any errors or problems regardless of the size of data I send.

And the code for the server side is as follows:

  private void BackMeUp(object voice)
  {
    TcpClient Voice = (TcpClient)voice;
    Voice.ReceiveTimeout = 30000;
    NetworkStream DataStream = Voice.GetStream();
    try
    {            
      bool ShouldLoop = true;
      //int loops = 0;
      int loops = -1;
      byte[] input = new byte[2048];
      byte[] buffer = new byte[0];

      //while (ShouldLoop)
      while(loops != 0)
      {
        loops = DataStream.Read(input, 0, 2048);

        for (int x = 0; x < loops; x++)
        {                        
          Array.Resize(ref buffer, buffer.Length + 1);
          buffer[buffer.Length - 1] = input[x];                       
        }
        //if (loops < 2048)
        //{
          //ShouldLoop = false;
          //break;
        //}                    
      }

      while (true)
      {   
        StringReader Reader = new StringReader(new ASCIIEncoding().GetString(buffer, 0, buffer.Length));
        DataSet DS = new DataSet();
        DS.ReadXml(Reader);

        if (DS.Tables.Count > 0)
        {
          if (DS.Tables["Images"].Rows.Count > 0)
          {
            foreach (DataRow row in DS.Tables["Images"].Rows)
            {
                  //
            }
          }
        }

        string response = "Got it!";                    

        DataStream.Write(new ASCIIEncoding().GetBytes(response), 0, response.Length);
        DataStream.Flush();

        Voice.Close();
        break;                  
      }
    }
    catch (Exception Ex)
    {
      File.WriteAllText("Elog.txt", Ex.Message + " " + (Ex.InnerException != null ? Ex.InnerException.ToString() : " no Inner"));
      Voice.Close();
    }
  }

The server recieves the data fine, and closes the stream when it reaches the end, however the data is cut-off and I get an error when I try to rebuild the dataset.

I have the impression this has to do with the time it takes to send the stream, and I have played around with the Close and Flush commands but I feel like I’m just shooting in the dark. Any help would be appreciated.

Concise version of question: What factors are involved with a TcpListener that could cause a) the truncation of the stream. or b) premature closing of the stream prior to all bytes being read. When the listener in question is on a remote host rather than a local server.

  • 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-31T12:56:56+00:00Added an answer on May 31, 2026 at 12:56 pm

    The Read method doesn’t have to return the number of bytes that you requested, or the entire stream at once. Especially if the stream is slow, it will be returned in small chunks.

    Call the Read method repeatedly, and handle the data for each block that you get. The Read method returns zero when the stream is read to the end:

    buffer = new byte[4096];
    do {
      int read = DataStream.Read(buffer, 0, buffer.Length);
      if (read != 0) {
        // handle the first "read" bytes of the buffer (index 0 to read-1)
      }
    } while (read != 0);
    

    If you know that your buffer is enough for any stream, you can fill up the buffer and handle it afterwards:

    buffer = new byte[4096];
    int offset = 0;
    do {
      int read = DataStream.Read(buffer, offset, buffer.Length - offset);
      offset += read;
    } while (read != 0);
    // handle the first "offset" bytes of the buffer (index 0 to offset-1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java application that will run for a long time (essentially, it
So, essentially, we're writing a service-level application that can alter attributes of various user-level
I'm writing a computation expression that is essentially implementing a State monad and I'm
I'm writing a cache-eject method that essentially looks like this: while ( myHashSet.Count >
I'm writing an application which will display the current image seen by a camera
I have to write an application that is essentially a proxy server to handle
I'm writing a program that essentially stores a series of scenarios. These scenarios need
I'm essentially writing a little script that needs to save several different files as
I'm currently writing a library for matching specific words in content. Essentially the way
I am writing some JavaScript that what I essentially want to do is confirm

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.