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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:03:58+00:00 2026-05-14T06:03:58+00:00

We’re stuck with using buffers on the SocketAsyncEventArgs object. With the old socket method

  • 0

We’re stuck with using buffers on the SocketAsyncEventArgs object.

With the old socket method we’d cast our state object, like this:

clientState cs = (clientState)asyncResult.AsyncState;

However, the 3.5 framework is different.

With have strings arriving from the client in chunks and we can’t seem to work out how the buffers work so we can process an entire string when we find a char3.

Code at the moment:

private void ProcessReceive(SocketAsyncEventArgs e)
{
    string content = string.Empty;

    // Check if the remote host closed the connection.
    if (e.BytesTransferred > 0)
    {
        if (e.SocketError == SocketError.Success)
        {
            Socket s = e.UserToken as Socket;
            //asyncResult.AsyncState;

            Int32 bytesTransferred = e.BytesTransferred;

            // Get the message received from the listener.
            content += Encoding.ASCII.GetString(
                e.Buffer, e.Offset, bytesTransferred);

            if (content.IndexOf(Convert.ToString((char)3)) > -1)
            {
                e.BufferList = null;

                // Increment the count of the total bytes receive by the server
                Interlocked.Add(ref this.totalBytesRead, bytesTransferred);
            }
            else
            {
                content += Encoding.ASCII.GetString(
                    e.Buffer, e.Offset, bytesTransferred);
                ProcessReceive(e);
            }
        }
        else
        {
            this.CloseClientSocket(e);
        }
    }
}
  • 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-14T06:03:59+00:00Added an answer on May 14, 2026 at 6:03 am

    I’ll start by saying I’ve never worked with .NET 3.5 sockets, so this answer is a bit of an educated guess.

    The problem you are having is that you aren’t storing the content in some state on each read.
    So the problems are:
    Firstly

    1. You call ProcessReceive: string content = string.Empty;

    2. You append to content: ‘content += Encoding.ASCII.GetString(e.Buffer, e.Offset, ew.BytesTransferred);`

    3. You call ProcessReceive Again: string content = string.Empty;
    4. The cycle continues and content always gets set to an empty string.

    Secondly
    To receive the remaining data you are calling ProcessReceive, this is incorrect. You need to issue another read to the underlying Socket using ReceiveAsync.

    I’ve modified your code using this blog post.

    // You'll need some state object, if you don't already have one
    class AsyncServerState
    {
       public byte[] Buffer = new byte[8192]; // 8K buffer
       public StringBuilder Content = new StringBuilder0; // Place to store the content as it's received
       public SocketAsyncEventArgs ReadEventArgs = new SocketAsyncEventArgs();
       public Socket Client;
    }
    
    // You'll need to setup the state where ever you process your connect
    // something similar to this.
    void Accept_Completed(object sender, SocketAsyncEventArgs e)
    {
        if (e.SocketError == SocketError.Success)
        {
            Socket client = e.AcceptSocket;
            AsyncServerState state = new AsyncServerState();
            state.ReadEventArgs.AcceptSocket = client;
            state.ReadEventArgs.Completed += new EventHandler(                                              IO_Completed);
            state.ReadEventArgs.UserToken = state;
            state.Client = client;
            state.ReadEventArgs.SetBuffer(state.Buffer, 0, state.Buffer.Length);
    
            if (!client.ReceiveAsync(state.ReadEventArgs))
            {   
                // Call completed synchonously
                ProcessReceive(state.ReadEventArgs);
            }
        }
        ProcessAccept(e);
    }
    
    private void ProcessReceive(SocketAsyncEventArgs e)
    {        
        var state = e.UserToken as AsyncServerState;
    
        // Check if the remote host closed the connection.
        if (e.BytesTransferred > 0)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Get the message received from the listener.
                sting content = Encoding.ASCII.GetString(state.Buffer, 0, e.BytesTransferred);
    
                // Append the received data to our state
                state.Content.Append(content);                          
    
                // Increment the count of the total bytes receive by the server
                Interlocked.Add(ref this.totalBytesRead, bytesTransferred);
    
                if (content.IndexOf(Convert.ToString((char)3)) > -1)
                {
                    // Final Message stored in our state
                    string finalContent = state.Content.ToString();
                    return;                
                }
                else
                {
                    // You need to issue another ReceiveAsync, you can't just call ProcessReceive again
                    if (!state.Client.ReceiveAsync(state.ReadEventArgs))
                    {
                        // Call completed synchonously
                        ProcessReceive(state.ReadEventArgs);
                    }                
                }
            }
            else
            {
                this.CloseClientSocket(e);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • 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 The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

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.