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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:44:36+00:00 2026-05-13T09:44:36+00:00

I am new to programming and I am confused about asynchronous socket programming. For

  • 0

I am new to programming and I am confused about asynchronous socket programming.

For example let’s say I have two BeginSends, one right after another. The first sends a million chars and the second sends only 64 chars.

Due to the asynchronous nature won’t the second BeginSend complete before the first one?

If so how can I tell the recieved data at EndRecieve, belongs to which message?

  • 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-13T09:44:36+00:00Added an answer on May 13, 2026 at 9:44 am

    You really shouldn’t call two BeginSend’s after one another.
    I think that you’ll end up seeing exceptions thrown if this happens.

    You have to call an EndSend before calling another BeginSend. This is typically done in side the callback function.

    Check out the example Using an Asynchronous Client Socket at MSDN. In the callback the EndSend is called, and then a ManualResetEvent called “sendDone” is set. This is a form of inter-thread communication in which the callback is signaling to your main thread that the asynchronous send is completed. This then allows your program to cue up the next piece of data.

    • Call BeginSend from your main thread to send the first 1,000,000 bytes
    • Your main thread can check on a semaphore or something like the ManualResetEvent to trigger it to send the next 64 bytes. Your other option is to use a queue for the data to send
    • When the data has finished sending, the callback you passed in to the BeginSend will be called.
    • In this call back, you will call EndSend. Follow this then setting the ManualResetEvent, or what ever inter-thread trigger you wish to use.

    The simplest option, which I recall doing once, is to call BeginSend for the next piece of data in the callback for the first piece of data being done.

    e.g.

    int NumBytesSent; // member variable containing how many bytes you have sent so far
    string Message;   // Message to send that is large
    

    When you call BeginSend, pass in a chunk of say 100 bytes (pick a larger number for more efficient transfers, but not too large) to send and increment NumBytesSend by 100.
    Next, in your callback for the previous send being completed you can check if NumBytesSent < Message.Length, and if it is then send the next chunk of the Message.

    This is how you would send a file you send a bunch of bytes at once, and just keep sending chunks of the file until it is all sent.

    I highly recommend making a simple client/server to do something like sending a complete file over a connection. Also review the MSDN documents and the many other examples out and about in the web.

    Getting a good grip on this will help you with lots of other C# topics as working asynchronously (or using delegates/callbacks) is very common for .NET

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

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 293k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer IE has a habit of caching AJAX requests. Try something… May 13, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer If you use Authlogic, you could be using the Single… May 13, 2026 at 6:20 pm
  • Editorial Team
    Editorial Team added an answer This works in IE. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0… May 13, 2026 at 6:20 pm

Related Questions

I am new to programming and come from a non-CS background (no formal degree).
I've recently started down the road of programming in Objective-C, and I'm now looking
I am an inexperienced programmer , completely new to programming for windows . I
I am working through some of the exercises in The C++ Programming Language by
Ok, I'm programming in objective-C and using Xcode. I have read through the documentation

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.