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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:40:33+00:00 2026-05-11T09:40:33+00:00

I’m looking at the source code for a server using SocketAsyncEventArgs, and I’m trying

  • 0

I’m looking at the source code for a server using SocketAsyncEventArgs, and I’m trying to figure out how this wouldn’t cause a stack overflow:

So this code is called to allow the socket to accept an incoming connection (scroll down to the bottom to see what I mean):

/// <summary> /// Begins an operation to accept a connection request from the client. /// </summary> /// <param name='acceptEventArg'>The context object to use when issuing  /// the accept operation on the server's listening socket.</param> private void StartAccept(SocketAsyncEventArgs acceptEventArg) {     if (acceptEventArg == null)     {         acceptEventArg = new SocketAsyncEventArgs();         acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(OnAcceptCompleted);     }     else     {         // Socket must be cleared since the context object is being reused.         acceptEventArg.AcceptSocket = null;     }      this.semaphoreAcceptedClients.WaitOne();     Boolean willRaiseEvent = this.listenSocket.AcceptAsync(acceptEventArg);     if (!willRaiseEvent)     {         this.ProcessAccept(acceptEventArg);     } } 

Then this code gets called once a connection is actually accepted (see last line):

  /// <summary>         /// Process the accept for the socket listener.         /// </summary>         /// <param name='e'>SocketAsyncEventArg associated with the completed accept operation.</param>         private void ProcessAccept(SocketAsyncEventArgs e)         {             if (e.BytesTransferred > 0)             {                 Interlocked.Increment(ref this.numConnectedSockets);                 Console.WriteLine('Client connection accepted. There are {0} clients connected to the server',                     this.numConnectedSockets);             }              // Get the socket for the accepted client connection and put it into the              // ReadEventArg object user token.             SocketAsyncEventArgs readEventArgs = this.readWritePool.Pop();             readEventArgs.UserToken = e.AcceptSocket;              // As soon as the client is connected, post a receive to the connection.             Boolean willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);             if (!willRaiseEvent)             {                 this.ProcessReceive(readEventArgs);             }              // Accept the next connection request.             this.StartAccept(e); // <==== tail end recursive?         } 

Look at the last line. It calls the top function again. How does this not overflow the stack by ping-ponging back and forth between these 2 functions? It seems to be tail end recursion, but this isn’t Haskell so I don’t see how this would work.

It was my understanding that these weren’t fired in threads but where just executed one at a time by the cpu.

  • 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. 2026-05-11T09:40:33+00:00Added an answer on May 11, 2026 at 9:40 am

    If the AsyncAccept (or any AsyncXXX operation for that matter) cannot be satisfied immediately, then it will return true, indicating that the operation will complete asynchronously. When this happens, the callback-event will ultimately fire on a thread-pool thread. Even if it marshal’s back to a UI thread (because it was initiated there), it will do so via a post.

    AsyncAccept is highly likely to return true, because unless there are socket connections truly pending (see backlog in Listen), you are waiting for a client to connect.

    Hence, StartAccept() will simply exit without calling ProcessAccept, and ProcessAccept when (and if) it fires, will probably be on a different thread.

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

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You want $method['test3'] to access the elements of the $_POST… May 11, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer In a utility class file. May 11, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer As indicated in the online manual, the most common cause… May 11, 2026 at 9:17 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.