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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:18:08+00:00 2026-05-25T17:18:08+00:00

Normally I can do something like this to fill up the byte array with

  • 0

Normally I can do something like this to fill up the byte array with stream data:

byte[] dataLength = new byte[4];

clientStream.Read(dataLength, 0, dataLength.Length);

And that fills up the byte array.. However, I’ve been experimenting with async calls and my code looks like this:

byte[] dataLength = new byte[4];

clientStream.BeginRead(dataLength, 0, dataLength.Length, Read, clientStream);

private void Read(IAsyncResult async)
{
    NetworkStream clientStream = (NetworkStream)async.AsyncState;

    clientStream.EndRead(async);

    byte[] dataLength = new byte[4]; // ..?

    clientStream.Read(dataLength, 0, dataLength.Length); // Have to re-read in data with synchronous version?..

    int result = BitConverter.ToInt32(dataLength, 0);
}

Which I feel is completely.. wrong. What is the point of the async call if you just have to read it all over again in the callback synchronously? How can I access the already read-in bytes without making the dataLength a member variable of the class? Obviously I don’t want to do that because there is more than one connection and they all have different values..

I feel like I’m missing something obvious here..

  • 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-25T17:18:08+00:00Added an answer on May 25, 2026 at 5:18 pm

    You do not have to read it all over again – when you call

    clientStream.EndRead(async);
    

    it returns the number of bytes that have been read, so you will want to do:

    int bytesRead = clientStream.EndRead(async);
    

    At this point your buffer has been filled with those bytes, reading from the stream in a synchronous fashion would just read more bytes.

    If you do not want to make your buffer an instance variable you could use a closure with a delegate instead:

    byte[] buffer = new byte[4];
    clientStream.BeginRead(buffer, 0, buffer.Length, (IAsyncResult async) =>
    {
        int bytesRead = clientStream.EndRead(async);
        if (bytesRead == 4)
        {
            int result = BitConverter.ToInt32(buffer, 0);
            //..
        }
    }, clientStream);
    

    Edit:

    A better solution might be to put all the state in form of a custom class and pass it in BeginRead():

    public class StreamState
    {
        public byte[] Buffer { get; set; }
        public NetworkStream Stream { get; set; }
    }
    
    clientStream.BeginRead(buffer, 0, buffer.Length, Read, new StreamState { Buffer = buffer, Stream = clientStream });
    
    
    private void Read(IAsyncResult async)
    {
        StreamState state = (StreamState) async.AsyncState;
        int bytesRead = state.Stream.EndRead(async);
        if (bytesRead == 4)
        {
            int result = BitConverter.ToInt32(state.Buffer, 0);
            //..
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally you can do something like this for properties: public String s {get; set;
Normally, we can create properties like this, dynamic expando = new ExpandoObject(); expando.Price =
Normally, the UITableView is something like this: [ cell 1 ] [ cell 2
Looking to create an inline array in Visual Basic for Applications Something like this
Normally you can print a string in C like this.. printf(No record with name
Normally I make something like this. #1 first query while () : #2 2nd
Normally when you iterate through a mysql result you do something like this: while
I often want to write something like this: new Form { Text = Caption,
I know normally you can use pagination like this: class SearchController < ApplicationController def
normally my ajax functions look something like this: function ajaxCallback(url,functionToRun) { if (window.XMLHttpRequest) {//

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.