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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:54:29+00:00 2026-06-14T14:54:29+00:00

I’m trying to set up some Network stuff. For this reason I need to

  • 0

I’m trying to set up some Network stuff. For this reason I need to send and receive data over network. In particular HTTP-Messages. I want to implement this with asynchronous Methods in C#. When im receiving the HTTP-Response from a Webserver, I want to receive data until the webserver closed the connection or (connection: keep alive) until i received all bytes of the HTTP-Response. I already tried to set this up method but failed. Can you tell me how to implement this. I’ve seen this example on msdn:

    private static void Receive(Socket client) {
    try {
        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = client;

        // Begin receiving the data from the remote device.
        client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback), state);
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

private static void ReceiveCallback( IAsyncResult ar ) {
    try {
        // Retrieve the state object and the client socket 
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket client = state.workSocket;

        // Read data from the remote device.
        int bytesRead = client.EndReceive(ar);

        if (bytesRead > 0) {
            // There might be more data, so store the data received so far.
        state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

            // Get the rest of the data.
            client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
                new AsyncCallback(ReceiveCallback), state);
        } else {
            // All the data has arrived; put it in response.
            if (state.sb.Length > 1) {
                response = state.sb.ToString();
            }
            // Signal that all bytes have been received.
            receiveDone.Set();
        }
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

With the State Object:

public class StateObject {
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}

If I would use similar code to this, I’m afraid to run into horrible sideeffects because all Threads use the same state-object. Furthermore, I have to resend the data received data immediately.

  • 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-14T14:54:30+00:00Added an answer on June 14, 2026 at 2:54 pm

    I tried to implement the asynchronous Receive method. Well, its not finished yet, but the basic structure should be clear.

    private void BeginReceiveFromWebserver(ClientSocket clientSocket)
        {
            try
            {
                clientSocket.CommunicationSocket.BeginReceive(clientSocket.Buffer, 0, clientSocket.Buffer.Length,
                                                             SocketFlags.None, new AsyncCallback(ReceiveFromWebserverCallback), clientSocket);
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\nfrom Source: " + e.Source + "\nand Stack Trace: " + e.StackTrace);
                //XXX TODO
            }
        }
    
        private void ReceiveFromWebserverCallback(IAsyncResult ar)
        {            
            ClientSocket clientSocket = ar.AsyncState as ClientSocket;
    
            try
            {
                int bytesRead = clientSocket.CommunicationSocket.EndReceive(ar);
    
                if (bytesRead > 0)
                {
                    if(!clientSocket.AllBytesReceived)
                    {
                        // No locks needed here, because it works like a loop
                        clientSocket.TotalBytesReceived+=bytesRead;
                        clientSocket.BufferUsed++;
                        clientSocket.AddBuffer();
                        clientSocket.SendingToBrowserCompleted.WaitOne();
                        BeginSend(clientSocket, bytesRead);
                        clientSocket.CommunicationSocket.BeginReceive(clientSocket.Buffer, 0, clientSocket.Buffer.Length,
                                                            SocketFlags.None, new AsyncCallback(ReceiveFromWebserverCallback), clientSocket);
                    }
                    else
                    {
                        // all bytes Received
                    }
                }
                else
                {
                    Console.WriteLine("Webserver Closed Connection");
                    //XXX TODO, Webserver closed the Connection
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\nfrom Source: " + e.Source + "\nand Stack Trace: " + e.StackTrace);
                //XXX TODO
            }
        }
    

    Every peace of the message received, is resendet to Browser. To ensure that the Browser gets those peaces in the right order, I’ve got the SendingToBrowserCompleted-AutoResetEvent here. If your interested in more code, just ask.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.