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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:10:11+00:00 2026-06-08T12:10:11+00:00

I originally had a race condition when sending data, the issue was that I

  • 0

I originally had a race condition when sending data, the issue was that I was allowing multiple SocketAsyncEventArgs to be used to send data, but the first packet didn’t send fully before the 2nd packet, this is because I have it so if the data doesn’t fit in the buffer it loops until all the data is sent, and the first packet was larger than the second packet which is tiny, so the second packet was being sent and reached to the client before the first packet.

I have solved this by assigning 1 SocketAyncEventArgs to an open connection to be used for sending data and used a Semaphore to limit the access to it, and make the SocketAsyncEventArgs call back once it completed.

Now this works fine because all data is sent, calls back when its complete ready for the next send. The issue with this is, its causing blocking when I want to send data randomly to the open connection, and when there is a lot of data sending its going to block my threads.

I am looking for a work around to this, I thought of having a Queue which when data is requested to be sent, it simply adds the packet to the Queue and then 1 SocketAsyncEventArgs simply loops to send that data.

But how can I do this efficiently whilst still being scalable? I want to avoid blocking as much as I can whilst sending my packets in the order they are requested to be sent in.

Appreciate any help!

  • 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-08T12:10:12+00:00Added an answer on June 8, 2026 at 12:10 pm

    If the data needs to be kept in order, and you don’t want to block, then you need to add a queue. The way I do this is by tracking, on my state object, whether we already have an active send async-loop in process for that connection. After enqueue (which obviously must be synchronized), just check what is in-progress:

        public void PromptToSend(NetContext context)
        {
            if(Interlocked.CompareExchange(ref writerCount, 1, 0) == 0)
            { // then **we** are the writer
                context.Handler.StartSending(this);
            }
        }
    

    Here writerCount is the count of write-loops (which should be exactly 1 or 0) on the connection; if there aren’t any, we start one.

    My StartSending tries to read from that connection’s queue; if it can do so, it does the usual SendAsync etc:

    if (!connection.Socket.SendAsync(args)) SendCompleted(args);
    

    (note that SendCompleted here is for the “sync” case; it would have got to SendCompleted via the event-model for the “async” case). SendCompleted repeats this “dequeue, try send async” step, obviously.

    The only thing left is to make sure that when we try to dequeue, we note the lack of action if we find nothing more to do:

            if (bufferedLength == 0)
            {  // nothing to do; report this worker as inactive
                Interlocked.Exchange(ref writerCount, 0);
                return 0;
            }
    

    Make sense?

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

Sidebar

Related Questions

Problem: I originally had a query that was working great but I'm now having
I originally had an array[1..1000] that was defined as a global variable. But now
A co-worker asked about some code like this that originally had templates in it.
I originally started this question in another thread, but that thread was sorta, kinda
I originally had a submit button. <input class=submit type=submit class=input value=Add name=command /> but
I originally had this code written as a series of if statements, but after
I'm working on adapting a jQuery slider that originally had 10 thumbnails. I only
I originally had a function that uses pow(), and I noticed it was returning
I originally had a code segment that iterated through rows of an Excel spreadsheet
I originally had a number of signal processing filter classes that were identical apart

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.