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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:28:54+00:00 2026-05-30T10:28:54+00:00

I have an asynchronous read method… private void read(IAsyncResult ar) { //Get the Server

  • 0

I have an asynchronous read method…

private void read(IAsyncResult ar) {
        //Get the Server State Object
        ServerState state = (ServerState)ar.AsyncState;

        //read from the socket
        int readCount = state.socket.EndReceive(ar);

        //check if reading is done, move on if so, trigger another read if not
        if (readCount > 0) {
            //purge the buffer and start another read
            state.purgeBuffer();
            state.socket.BeginReceive(state.buffer, 0, ServerState.bufferSize, 0, new AsyncCallback(read), state);
        }
        else {
            //all bytes have been read, dispatch the message
            dispatch(state);
        }
    }

The problem that I am having is that read is only 0 if the connection is closed. How do I say, this is the end of that message and pass the data on to the dispatcher, while leaving the socket open to accept new messages.

Thank you!

  • 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-30T10:28:55+00:00Added an answer on May 30, 2026 at 10:28 am

    You should not rely on what is in the TCP buffer. You must process the incoming bytes as a stream somewhere. You can’t really know whether its complete. Only one layer above can know when the message completed.

    Example:
    If you read HTTP responses the HTTP header will contain the byte count which is in the HTTP body. So you know how much to read.

    You only know how much to read if the data follows a certain protocol and you interprete it. Imagine you receive a file over the socket. The first thing you would receive is the file size. Without that you would never know how much to read.

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

Sidebar

Related Questions

My form receives asynchronous callbacks from another object on random worker threads. I have
I have not been able to find a way to cancel/terminate asynchronous read operation
I have read about executing asynchronous operations and i found that it can be
I am trying to fire a method as a result to an asynchronous read
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
I have a Server class which it basically waits for connections from a client.
I have a NetworkStream which I read asynchronously (using async/await) await Task<int>.Factory.FromAsync((cb, state) =>
I have a stateless EJB-3.1 session bean containing an asynchronous method that does some
I have few asynchronous tasks running and I need to wait until at least
I have been researching asynchronous messaging, and I like the way it elegantly deals

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.