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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:29:32+00:00 2026-05-24T00:29:32+00:00

Assume I have simple protocol implemented over TCP, where each message is made up

  • 0

Assume I have simple protocol implemented over TCP, where each message is made up of:

  1. An int indicating data length.
  2. Binary data, of the length specified in 1.

Reading such a message I would like something like:

int length = input.ReadInt();
byte[] data = input.ReadBytes(length);

Using Socket.Receive or NetworkStream.Read the available number of bytes is read. I want the call to ReadBytes to block until length bytes are available.

Is there a simple way to do this, without having to loop over the read, restarting at an offset waiting for the remaining data?

In a real application the read should probably be done Async or on a background thread, but I’ve ignored that for now. The important thing is to be able to have the read not complete until all data is available.

Edit

I know that I can buffer the data myself, and I know how to do it. It’s just a loop around Receive that continues at the next offset. What I am asking for is if there is a reusable implementation of such a loop, without the need for an own loop of any kind (or alternatively a reusable Async implemenation that finishes when all data is available).

  • 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-24T00:29:34+00:00Added an answer on May 24, 2026 at 12:29 am

    Something, somewhere is going to have to loop. After all, multiple socket reads could be required.

    I believe that BinaryReader.Read will keep looping until either it’s read as much as you’ve asked for or hit the end of the stream, but assuming you would want to throw an exception if you reached the end of the stream, I’d write personally write a separate method. It’s easy enough to implement in one place and reuse, after all.

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

Sidebar

Related Questions

Let's assume I have a simple XML file: <data> <text>Hello world!&lt;br&gt;Nice to see you
Assume we have a simple structure such as the following struct T{ int x;
Let's assume we have a simple internet socket, and it's going to send 10
Assume we have a simple @Configuration : @Configuration public class FooBarConfiguration { @Bean public
Assume I have such simple model: class Foo(models.Model): name = models.CharField(max_length=25) b_date = models.DateField()
Assume I have some simple class class TestClass: def doSomething(self): print 'Did something' I
Assume I have this simple core with generics as genertest.vhd : -------------------------------------------------------------------------- library IEEE;
Assume you have the following simple objects: class Order { public Customer[] Customers {
In the simple case, assume I have a table that looks like this: mysql>
Let's assume we have simple UI and for whatever reason, every 5 seconds a

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.