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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:21:11+00:00 2026-06-01T07:21:11+00:00

The data in question is a PNG file prefixed by it’s size as an

  • 0

The data in question is a PNG file prefixed by it’s size as an int.

-Sending:
ns.Write(BitConverter.GetBytes((int)data.Length),0,4);
ns.Write(data, 0, data.Length);

-Reading:
byte[] sizearray = new byte[4];
ns.Read(sizearray, 0, 4);
int dataSize = BitConverter.ToInt32(sizearray,0);

byte[] data = new byte[dataSize];
ns.Read(data, 0, dataSize);

The recieved data is then saved to a file. I’ve also tried this with BeginRead/EndRead with the same result.

The issue is, while this works for most smaller images, it doesn’t receive an image which is more then a few KB. The dataSize reads correctly, but after a few thousand bytes each time (~2900), the rest of the received data is 0. Example

Have I overlooked something, like a limit on how much can be sent at once?

  • 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-06-01T07:21:13+00:00Added an answer on June 1, 2026 at 7:21 am

    You’re ignoring the return value of Read. Don’t do that. Read doesn’t wait until it’s read all of the data you requested. You should read in a loop until you’ve read everything you need to:

    byte[] data = new byte[dataSize];
    int index = 0;
    while (index < dataSize)
    {
        int bytesRead = ns.Read(data, index, dataSize - index);
        if (bytesRead <= 0)
        {
            // Or whatever exception you want
            throw new InvalidDataException("Premature end of stream");
        }
        index += bytesRead;
    }
    

    In theory, you need to do the same even when reading dataSize, although in practice I doubt whether you’ll receive fewer than 4 bytes in one read.

    You might want to use a BinaryReader wrapped around the stream – then you could use ReadInt32 and ReadBytes, where ReadBytes will do the looping for you. (You’d still need to check the return value, but it would be simpler than doing it yourself.)

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

Sidebar

Related Questions

url = http://example.com/file.xml data = urllib2.urlopen(url) data.read() The question is, when exactly will the
I'm working on a solution to my other question which is reading the data
Dynamic Data question: I have 2 fields of type Nullable<DateTime> on my model When
my question is about two possible ways to access data: (My question is about
This is mostly a data warehouse philosophy question. My project involves an Oracle forms
So I got this question in data structures class' homework, I'm translating so I
This question discusses encrypting data on the iPhone using the crypt() function. As an
The question gives all necessary data: what is an efficient algorithm to generate a
This question is about a data structure I thought of. It is a dynamic
I was trying to save a PNG file to photos albums folder on the

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.