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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:07:07+00:00 2026-06-12T05:07:07+00:00

I just read an article that says TCPClient.Read() may not get all the sent

  • 0

I just read an article that says TCPClient.Read() may not get all the sent bytes in one read. How do you account for this?

For example, the server can write a string to the tcp stream. The client reads half of the string’s bytes, and then reads the other half in another read call.

how do you know when you need to combine the byte arrays received in both calls?

  • 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-12T05:07:08+00:00Added an answer on June 12, 2026 at 5:07 am

    how do you know when you need to combine the byte arrays received in both calls?

    You need to decide this at the protocol level. There are four common models:

    • Close-on-finish: each side can only send a single “message” per connection. After sending the message, they close the sending side of the socket. The receiving side keeps reading until it reaches the end of the stream.
    • Length-prefixing: Before each message, include the number of bytes in the message. This could be in a fixed-length format (e.g. always 4 bytes) or some compressed format (e.g. 7 bits of size data per byte, top bit set for the final byte of size data). Then there’s the message itself. The receiving code will read the size, then read that many bytes.
    • Chunking: Like length-prefixing, but in smaller chunks. Each chunk is length-prefixed, with a final chunk indicating “end of message”
    • End-of-message signal: Keep reading until you see the terminator for the message. This can be a pain if the message has to be able to include arbitrary data, as you’d need to include an escaping mechanism in order to represent the terminator data within the message.

    Additionally, less commonly, there are protocols where each message is always a particular size – in which case you just need to keep going until you’ve read that much data.

    In all of these cases, you basically need to loop, reading data into some sort of buffer until you’ve got enough of it, however you determine that. You should always use the return value of Read to note how many bytes you actually read, and always check whether it’s 0, in which case you’ve reached the end of the stream.

    Also note that this doesn’t just affect network streams – for anything other than a local MemoryStream (which will always read as much data as you ask for in one go, if it’s in the stream at all), you should assume that data may only become available over the course of multiple calls.

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

Sidebar

Related Questions

I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).
I just read this article from Google. I always thought that closing tags is
I just read an article and saw and only one MSIL instruction that loads
I just read an article that explains the zero-copy mechanism. It talks about the
I've just read an article that supposedly introduced me to a new concept: Up
I just read an article on www.songho.ca which indicates that a projection matrix is
I just read this article: http://www.acunetix.com/websitesecurity/upload-forms-threat.htm which discusses some of the security risks involved
I just read the Wikipedia page about Bucket sort . In this article they
I already read many article about this issue in here, SO. I just want
I just read in an article about JIT Compilation that Java has two modes

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.