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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:27:36+00:00 2026-05-28T21:27:36+00:00

I am trying to understand the ‘SocketAsyncEventArgs’ class in C#. http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx I am following

  • 0

I am trying to understand the ‘SocketAsyncEventArgs’ class in C#. http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

I am following this tutorial:
http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

Now I am stuck into how I exactly should process data with my server. What I am trying to do is use a SocketAsyncEventArgs for a connected client with an allocated Buffer space of 512 bytes in the BufferManager. Then what I want to do is to decode the byte[] data into my own custom class (ClientPacket) which holds the byte[] for decoding and reading from.

This is all well but my server doesn’t always respond with data the question is:

Do I use 1 SocketAsyncEventArgs for the receiving and loop around to process receive data, then allocate a SocketAsyncEventArgs from the pool whenever I need to send then return it on completion?

And how does the SocketAsyncEventArgs know when reading is complete? (when it is finished with the byte[] buffer before it over-writes it with new data) like how is it returned to the pool when done if I don’t respond back?

  • 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-28T21:27:36+00:00Added an answer on May 28, 2026 at 9:27 pm

    I only use one SocketAsyncEventArgs instance for all of my needs. I simply reset the buffer between each request (by setting it to a new Byte[]).

    Once I have connected and have a reference to the Socket, I start listening like this:

    public void StartListening(SocketAsyncEventArgs e)
    {
        ResetBuffer(e);
        e.Completed += SocketReceive;
    
        socket.ReceiveAsync(e);
    }
    

    I have a helper function that resets the buffer:

    private void ResetBuffer(SocketAsyncEventArgs e)
    {
        var buffer = new Byte[SocketBufferSize];
    
        e.SetBuffer(buffer, 0, SocketBufferSize);
    }
    

    I process the data like:

    private void SocketReceive(Object sender, SocketAsyncEventArgs e)
    {
        ProcessData(e.Buffer, 0, e.BytesTransferred);
    
        ResetBuffer(e);
    
        socket.ReceiveAsync(e);
    }
    

    In ProcessData, you can use the byte array as needed to pull in the data. I use it to create a MemoryStream which I then deserialize into my class (similar to ClientPacket), as follows:

    private void ProcessData(Byte[] data, Int32 count)
    {
        using (var stream = new MemoryStream(data, 0, count))
        {
            var serializer = new XmlSerializer(typeof(ClientPacket));
    
            var packet = serializer.Deserialize(stream);
    
            // Do something with the packet
        }
    }
    

    As for your last question. The framework handles everything to do with the underlying TCP protocol, etc. so you can rely on the event handler being called whenever there is data to be processed. Use the e.BytesTransferred value to indicate the amount of data you actually received which may be smaller than but will never exceed your buffer size (SocketBufferSize in my code). If the message was larger than the buffer size, the TCP infrastructure will buffer the messages and send them to you in chunks based on SocketBufferSize (by raising the event once for each chunk). If this is a concern, simply increase SocketBufferSize until the majority of your message are received in one chunk.

    The downside of the chunking is that messages may be merged by the infrastructure which means that you may need a way to tell when the first message has ended. Typical approaches include prefacing your message with a 4 byte integer that indicates the message length. I can elaborate more if needed.

    Hope that helps.

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

Sidebar

Related Questions

I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
trying to understand http and headers i was playing around with telnet to send
Im trying to understand what a functor is, i found this tutorial/example: http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors data
Trying to understand how CoffeeScript instance and class variable works I came with this
HI Trying to understand how __radd__ works. I have the code >>> class X(object):
Trying to understand radix sort for my data structures class. My teacher showed us
Trying to understand this MSDN sample but I'm confused about these lines: IAsyncResult result
Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the

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.