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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:03:45+00:00 2026-05-11T07:03:45+00:00

When reading data in chunks of say, 1024, how do I continue to read

  • 0

When reading data in chunks of say, 1024, how do I continue to read from a socket that receives a message bigger than 1024 bytes until there is no data left? Should I just use BeginReceive to read a packet’s length prefix only, and then once that is retrieved, use Receive() (in the async thread) to read the rest of the packet? Or is there another way?

edit:

I thought Jon Skeet’s link had the solution, but there is a bit of a speedbump with that code. The code I used is:

public class StateObject {     public Socket workSocket = null;     public const int BUFFER_SIZE = 1024;     public byte[] buffer = new byte[BUFFER_SIZE];     public StringBuilder sb = new StringBuilder(); }  public static void Read_Callback(IAsyncResult ar) {     StateObject so = (StateObject) ar.AsyncState;     Socket s = so.workSocket;          int read = s.EndReceive(ar);          if (read > 0)      {         so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));          if (read == StateObject.BUFFER_SIZE)         {             s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,                      new AyncCallback(Async_Send_Receive.Read_Callback), so);             return;         }     }          if (so.sb.Length > 0)     {         //All of the data has been read, so displays it to the console         string strContent;         strContent = so.sb.ToString();         Console.WriteLine(String.Format("Read {0} byte from socket" +          "data = {1} ", strContent.Length, strContent));     }     s.Close(); } 

Now this corrected works fine most of the time, but it fails when the packet’s size is a multiple of the buffer. The reason for this is if the buffer gets filled on a read it is assumed there is more data; but the same problem happens as before. A 2 byte buffer, for exmaple, gets filled twice on a 4 byte packet, and assumes there is more data. It then blocks because there is nothing left to read. The problem is that the receive function doesn’t know when the end of the packet is.


This got me thinking to two possible solutions: I could either have an end-of-packet delimiter or I could read the packet header to find the length and then receive exactly that amount (as I originally suggested).

There’s problems with each of these, though. I don’t like the idea of using a delimiter, as a user could somehow work that into a packet in an input string from the app and screw it up. It also just seems kinda sloppy to me.

The length header sounds ok, but I’m planning on using protocol buffers – I don’t know the format of the data. Is there a length header? How many bytes is it? Would this be something I implement myself? Etc..

What should I do?

  • 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. 2026-05-11T07:03:46+00:00Added an answer on May 11, 2026 at 7:03 am

    No – call BeginReceive again from the callback handler, until EndReceive returns 0. Basically, you should keep on receiving asynchronously, assuming you want the fullest benefit of asynchronous IO.

    If you look at the MSDN page for Socket.BeginReceive you’ll see an example of this. (Admittedly it’s not as easy to follow as it might be.)

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It may be too early to tell, because the behavior… May 11, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer The problem was that the path did not exist.. :(… May 11, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer As I misinterpreted your intention the first time. Here the… May 11, 2026 at 9:56 pm

Related Questions

Are there any good language-agnostic distributed make systems for linux that are secure and
We are in evaluating technologies that we'll use to store data that we gather
I need to parse a xml file which is practically an image of a
Ok, I'm reading data from a stream using a StreamReader. The data inside the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.