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

The Archive Base Latest Questions

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

This is similar to my last question. I’m making a simple tcp/ip chat program

  • 0

This is similar to my last question. I’m making a simple tcp/ip chat program and I’m having a little difficulty with the EndReceive Callback function. I pasted in Microsoft’s implementation (see below) and what I’ve noticed is that if the message has been read in it’s entirety it won’t run the callback function until the next message is sent i.e. EndReceive will never return a 0. For example if I send a text message of 5 characters and the buffer size is 20, ‘read’ will be 5, I’ll process the string and call BeginReceive, however BeginReceive doesn’t run until the server sends another message. The else section is never reached because s.EndReceive never returns a 0. How would I know when data has been received completely?

public static void Read_Callback(IAsyncResult ar){
 StateObject so = (StateObject) ar.AsyncState;
 Socket s = so.workSocket;

 int read = s.EndReceive(ar);

 if (read > 0) {
            so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
            s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, 
                                  new AsyncCallback(Async_Send_Receive.Read_Callback), so);
 }
 else{
      if (so.sb.Length > 1) {
           //All of the data has been read, so displays it to the console
           string strContent;
           strContent = so.sb.ToString();
           Console.WriteLine(String.Format("Read {0} byte from socket" + 
                           "data = {1} ", strContent.Length, strContent));
      }
      s.Close();
 }
}
  • 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-19T00:24:47+00:00Added an answer on May 19, 2026 at 12:24 am

    EndReceive may well get a 0 if the stream is closed and all data has been consumed. However, for most other purposes there is no logical break in a stream – and even NetworkStream.DataAvailable only tells you what is available at the client – not what was sent by the server. Hence: if you are sending multiple messages / etc on the same stream it is usually necessary to either prefix each block with the number of bytes expected, or to use some delimiter sequence that would not normally be expected in the middle of content. The former is often easier if the content could be arbitrary binary content (i.e. it is tricky to predict a sequence that isn’t expected), but the latter allows streaming without knowing the lengths ahead of time (useful if you are computing the content on-the-fly, as otherwise you may need to buffer the content first to know the length (or do a dry run discarding the data).

    Also note that Read is not guaranteed to return all the available data – it would be entirely legitimate to send "HELLO", and have Read return just 1 (the H). Hence you should expect to have to combine multiple Read calls into a single message (again, using the length-prefix or the expected delimiter sequence).

    In this scenario, I’d be tempted to send HELLO\0 (where \0 is a zero-byte). Then I would consume via Read until either it returns <=0, or a \0 is found. In the \0 case, anything buffered before the terminator represents a single message; anything after the delimiter should be stored and processed as part of the next message-or-messages. As a test case, imagine you get Read return 13, with sequence a\0b\0cde\0f\0\0\0g, which which should "a", "b", "cde", "f", "", "", "g" (noting that the g should not be processed – it might be part of a longer sequences that isn’t yet received – buffer until you get a \0).

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

Sidebar

Related Questions

This is similar to this question , but I want to include the path
This is similar to a previous question , but the answers there don't satisfy
I know this is similar to this question , but I'm using SQL Server
Very similar to this question , except for Java. What is the recommended way
Similar to this question , after running the following code the browser dialog does
Similar to this question ... What are the worst practices you actually found in
Similar to this question, but for VB.NET since I learned this is a language
This is similar to my previous posting. But this time I want to call
This is similar to the cutting stock problem , but with a slight difference.
This is perhaps similar to previous posts, but I want to be specific about

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.