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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:03+00:00 2026-06-13T09:47:03+00:00

I am trying to write up a TcpClient implementation. I wanted to get the

  • 0

I am trying to write up a TcpClient implementation. I wanted to get the next read started while I was processing my data, but I am unsure of when the byte array I pass in to BeginRead is modified. Here is my current implementation

private void ProcessData()
{
    byte[] buffer = new byte[_tcpClient.ReceiveBufferSize];
    var networkStream = _tcpClient.GetStream();

    IAsyncResult result = networkStream.BeginRead(buffer, 0, buffer.Length, null, null);

    int read;
    while ((read = networkStream.EndRead(result)) != 0)
    {
        result = networkStream.BeginRead(buffer, 0, buffer.Length, null, null);

        ProcessChunk(buffer, read);
    }  
}

I started the next BeginRead before the long running function because I wanted the background IO to start getting the next chunk of data while I was processing. Can I do this with only one byte[] buffer, or do I need to alternate every BeginRead call?

private void ProcessData()
{
    bool bufferChoice = false;
    byte[] bufferA = new byte[_tcpClient.ReceiveBufferSize];
    byte[] bufferB = new byte[_tcpClient.ReceiveBufferSize];
    var networkStream = _tcpClient.GetStream();

    IAsyncResult result = networkStream.BeginRead(bufferA, 0, bufferA.Length, null, null);

    int read;
    while ((read = networkStream.EndRead(result)) != 0)
    {
        if (bufferChoice)
        {
            result = networkStream.BeginRead(bufferA, 0, bufferA.Length, null, null);
            ProcessChunk(bufferB, read); //Call function with B's buffer
        }
        else
        {
            result = networkStream.BeginRead(bufferB, 0, bufferB.Length, null, null);
            ProcessChunk(bufferA, read); //Call function with A's buffer
        }

        bufferChoice = !bufferChoice;
    }
}
  • 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-13T09:47:04+00:00Added an answer on June 13, 2026 at 9:47 am

    There are generally no guarantees when buffer passed to BeginRead will be used. Theoretically it may be even synchronously filled during BeginRead call.

    Side note: you should write code that is always behave correctly as it is so much easier to reason about than guess on particular behavior of particular implementation. In this case simply copy data or use multiple buffers so you never have outstanding IO and processing sharing the same buffer.

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

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
I am trying to implement a Client/Server model using TCpClient , with its Networkstream.Write()/Read()
I am trying to write a basic app that will read data off a
Trying to write out syslog entries containing strings but they don't register. // person.name
I get the following message back when trying to retrieve a file using TCPClient
I'm currently trying write code that will maintain the sorting preference while changing page
I'm trying to write out a Byte[] array representing a complete file to a
I'm trying to write a VB.net client that reads HTTP chunked data. The chunks
I'm trying write simple notify app in bash. I want to read output from
I am trying write a script where when a user clicks on a link,

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.