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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:12:59+00:00 2026-06-15T12:12:59+00:00

Possible Duplicate: Is the received stream from a socket limited to a single send

  • 0

Possible Duplicate:
Is the received stream from a socket limited to a single send command?

Note: I see this question very complicated (hopefully not for you guys, that’s why Im asking here lol) and I tried my best to explain it as simple and clear as possible.

In my application, I’m continually receiving byte arrays in a fix sized buffer.

These series of byte arrays that I’m receiving has been serialized ‘binarily’.

However, sometimes the byte array received will be bigger than the fix sized buffer so I would need to store the current received byte array into a container and loop again to receive the remaining byte arrays coming in.

My question now is how to “concatenate” or “combine” or “join” all the “batches” of byte arrays I received ( and is stored in a container, possibly a queue of byte arrays) to form a single byte array and then de-serialize them?

int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
    {
        // There might be more data, so store the data received so far.
        // If the buffer was not filled, I have to get the number of bytes received as Thorsten Dittmar was saying, before queuing it
        dataReceivedQueue.Enqueue(state.buffer);

        // Get the rest of the data.
        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
        new AsyncCallback(ReceiveCallback_onQuery), state);
    }
else
{
    // All the data has arrived; put it in response.
    response_onQueryHistory = ByteArrayToObject(functionThatCombinesBytes(dataReceivedQueue));

    // Signal that all bytes have been received.
    receiveDoneQuery.Set();
}

state.buffer is buffer where data are received. buffer is a byte array of size 4096. state is of type StateObject.

ByteArrayToObject(byte []) takes care of deserializing the data received and converting it back to its object form

functionThatCombinesBytes(Queue) this function will receive a Queue of bytes and will “combine” all the bytes into one byte array

  • 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-15T12:13:00+00:00Added an answer on June 15, 2026 at 12:13 pm

    Just because you are calling BeginReceive with a buffer of a particular size, doesn’t mean that it will necessarily entirely fill the buffer, so it’s very likely that some of your queued buffers will actually only be partially filled with received data, and the remainder being zeros, this will almost certainly corrupt your combined stream if you simply concatenate them together since you’re not also storing the number of bytes actually read into the buffer. You also appear to be reusing the same buffer each time, so you’ll just be overwriting already-read data with new data.

    I would therefore suggest replacing your dataReceivedQueue with a MemoryStream, and using something like:

    if (bytesRead > 0)
        {
            // There might be more data, so store the data received so far.
            memoryStream.Write(state.buffer, 0, bytesRead);
    
            // Get the rest of the data.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback_onQuery), state);
        }
    else
    {
        // All the data has arrived; put it in response.
        response_onQueryHistory = ByteArrayToObject(memoryStream.ToArray());
    
        // Signal that all bytes have been received.
        receiveDoneQuery.Set();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Emails showing from host.myserver.com instead of mydomain.com Not sure if this will
Possible Duplicate: How to send an e-mail from a Python script that is being
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Possible Duplicate: Push notifications - catching them? The question is : the iOS receives
Possible Duplicate: std::string x(x); class A {}; int main() { A a(a); } This
Possible Duplicate: How to programmatically send SMS on the iPhone? i am new to
Possible Duplicate: Program received signal: 0. Data Formatters temporarily unavailable I am taking above
Possible Duplicate: I need a good way to get data from a thread to

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.