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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:08:22+00:00 2026-05-24T20:08:22+00:00

I have a problem with the SocketAsyncEventArgs class..the problem is when I try to

  • 0

I have a problem with the SocketAsyncEventArgs class..the problem is when I try to send 8K of data
for example over internet, the socket sometimes only sends 1K or 2K, I know that this is normal for
TCP socket and that one send doesn’t guarantee one receive.
now for this to work for me I modified my code to resend the remaining data, for example
when I the SocketAsyncEventArgs.SendAsync completes, in the callback i check whether it sent all the 8K or not if it’s not, I call the SocketAsyncEventArgs.SendAsync again with the remaining data untill
I send it all.
Now, when I looked at some SocketAsyncEventArgs code.. I saw that most people don’t do so!
and they just clean up when the send complete without checking if it sent all the data or not!
also when I looked at Microsoft’s example, they were saying the ONE call to SocketAsyncEventArgs.SendAsync guarantees that all the data will be sent.
I mean I tested it myself and NO all data will not be sent in one call to SocketAsyncEventArgs.SendAsync.
What I’m doing wrong ?
thanks in advance.

Edit:
Here is the code which doesn’t send all data(exactly like microsoft’s)
the SendAsyncComplete will be called when the socket sends for example 1Kb of data
not all 8K!

public virtual void Send(byte[] packet, int offset, int length)
{
    if (_tcpSock != null && _tcpSock.Connected)
    {
        var args = SocketHelpers.AcquireSocketArg();
        if (args != null)
        {
            args.Completed += SendAsyncComplete;
            args.SetBuffer(packet, offset, length);
            args.UserToken = this;

            var willRaiseEvent = _tcpSock.SendAsync(args);
            if (!willRaiseEvent)
            {
            ProcessSend(args);
            }
            unchecked
            {
                _bytesSent += (uint)length;
            }
            Interlocked.Add(ref _totalBytesSent, length);
        }
        else
        {
            log.Error("Client {0}'s SocketArgs are null", this);
        }
    }
}

private static void ProcessSend(SocketAsyncEventArgs args)
{
      args.Completed -= SendAsyncComplete;
      SocketHelpers.ReleaseSocketArg(args);   
}

private static void SendAsyncComplete(object sender, SocketAsyncEventArgs args)
{
    ProcessSend(args);
}
  • 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-24T20:08:23+00:00Added an answer on May 24, 2026 at 8:08 pm

    There are many things I would change there. As a preamble, read this.

    First of all, whenever you sent any data on the socket, you must process that event: either you stop the whole send process, or you issue another socket send operation to send the remaining data.

    So it makes sense to have 3 methods, like this:

    // This is the Send() to be used by your class' clients
    1) public void Send(byte[] buffer);
    

    This method will take care to apply any data formatting that you need, create (retrieve) a SocketAsyncEventArgs object, set the token to hold your buffer, and call the next method bellow:

    2) private void Send(SocketAsyncEventArgs e);
    

    This one actually calls
    Socket.SendAsync(SocketAsyncEventArgs e)
    and copies the contents from the token (the buffer, remember?) to the SAEA object. Now that is why because method number (2) might be called several times to send the remaining data that couldn’t be sent in one operation by the socket. So here you copy the remaining data from the token to the SAEA buffer.

    3) private void ProcessSent(SocketAsyncEventArgs e);
    

    This last method will examine the data that has been sent by the socket. If all the data has been sent, the SAEA object will be released. If not, method (2) will be called again for the rest of the data. In order to keep track of sent data, you use SAEA.BytesTransferred. You should add this value to a value stored in the custom token I advise you to create (so do not use “this” as a token).

    This is where you also check for SocketError on the SAEA parameter.

    This last method will be called in two places:

    • in the 2nd method, like this:

          // Attempt to send data in an asynchronous fashion
          bool isAsync = this.Socket.SendAsync(e);
      
          // Something went wrong and we didn't send the data async
          if (!isAsync)
              this.ProcessSent(e);
      

    This bit is important any many people missed it even when using the more traditional Begin/EndXXX pattern (in that case, via IAsyncResult). If you don’t place it, once in a while (quite rare), a StackOverflow exception will pop out of nowhere and will keep you puzzled for long time.

    • in the Completed event handler:

      private void Completed(object sender, SocketAsyncEventArgs e)
      {
          // What type of operation did just completed?
          switch (e.LastOperation)
          {
              case SocketAsyncOperation.Send:
                  {
                      ProcessSent(e);
                      break;
                  }
          }
      }
      

    The tricky thing is to use one SocketAsyncEventArgs object per 1st Send(byte[]) operation, and release it in 3rd operation, if all data has been sent.

    For that, you must create a custom token (class or immutable struct) to place in SocketAsyncEventArgs.UserToken inside the 1st method, and then keep track of how much data you have transferred on each Socket.SendAsync() operation.

    When you are reading the article provided in the beginning, notice how the writer reuses the same SAEA object when at the end of Send() operation proceeds with a Receive() operation, if all the data has been sent. That is because his protocol is: each one of the parties (server and client) talk to each other in turns.

    Now, should multiple calls to the 1st Send() method occur at the same time, there is no rule in which order they will be handled by the OS. If that is likely to happen and message order is important, and since any call to Send(byte[]) from an “outside entity” result in a Socket.SendAsync(), I suggest that the 1st method actually writes down the received bytes in an internal buffer. As long as this buffer is not empty, you keep sending this data internally. Think of it like a producer-consumer scenario, where the “outside entity” is the producer and the internal send op is the consumer. I prefer an optimistic concurrency scenario here.

    The documentation on the matter is rather shallow, with the exception of this article, and even in this case, when you start implementing your own application, some things turn out to be different. This SocketAsyncEventArgs model is arguably a bit counter-intuitive.

    Let me know if you need more help, I had my times of struggle with this a while ago when I developed my own library.

    Edit:

    If I were you, I would move

    unchecked
    {
        _bytesSent += (uint)length;
    }
    Interlocked.Add(ref _totalBytesSent, length);
    

    to your ProcessSend(SAEA), and use args.BytesTransferred instead of “length”.

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

Sidebar

Related Questions

I have problem when I try insert some data to Informix TEXT column via
I have problem with databinding in the Win.Forms DataGridView control. Example: public class A
i have problem passing data from one page to another using GET, for example
i have problem with maven. When i try to compile test classes ( mvn
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem about django template-db query relationship. For example, I created two tables
I have problem with encoding data in Oracle database. I want to xor string
i have problem with enum I need make a enum in base class or
I have problem with the third level of a drop down menu, which 'over-passes'
I have problem in C#. I have a list box and a class called

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.