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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:46:14+00:00 2026-05-13T20:46:14+00:00

I am trying to learn and writting this part of code. while testing few

  • 0

I am trying to learn and writting this part of code.

while testing few weeks before it ran the way i wanted, it does not work its code from msdn and the http response part was being suggested by feroz
Using HttpWebRequest to send HTML to a Browser

now after a while it does not work..

i was expecting to get hello there message as in code which could be by using (StreamWriter sw = new StreamWriter(stream)) { sw.Write("<html><body>Hello There!</body></html>"); } but it only shows GET\

     try
     {

        // Set the TcpListener on port 13000.
            Int32 port = 80;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            TcpListener server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine(String.Format("Received: {0}", data));

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("Sending message..");


    using(StreamWriter sw = new StreamWriter(stream))
    {
        sw.Write("<html><body>Hello There!</body></html>");
    }

                 }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }

        Console.WriteLine("\nHit enter to continue...");
        Console.Read();
    }
}
  • 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-13T20:46:15+00:00Added an answer on May 13, 2026 at 8:46 pm

    The code presented in the question will throw ObjectDisposedException after the first read from the network. What you are doing “wrong” is disposing the StreamWriter (implicit in the using statement), which disposes the underlying network stream. Once disposed, you cannot go back and read from it anymore. Additionally, you are mixing writing directly to the stream and writing through the (buffered) StreamWriter.

    The question should be re-phrased to something like:

    **

    Why does this code read/write data
    from a client once, then throw an
    exception on the second read attempt?

    **

    I would restructure the code as shown below. [Note: The Flush() call is not really needed, but you probably want it in there for this demo.]

    try
    {
        // Listen for connections on port 13000
        TcpListener server = new TcpListener(IPAddress.Loopback, 13000);
        server.Start();
    
        // Read up tp 256 bytes at a time 
        Byte[] bytes = new Byte[256];
        String data;
    
        // Enter the listening loop. 
        while (true)
        {
            Console.WriteLine("Waiting for a connection... ");
    
            // Wait for a client connection
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Connected!");
    
            // Setup I/O streams
            NetworkStream stream = client.GetStream();
            using (StreamWriter sw = new StreamWriter(stream))
            {
                int i;
    
                // Loop to receive all the data sent by the client. 
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string. 
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine(String.Format("Received: {0}", data));
    
                    // Process the data sent by the client. 
                    data = data.ToUpper();
    
                    // Send back a response. 
                    Console.WriteLine("Sending message..");
                    sw.Write(data);
    
                    // Add a little extra 'response'
                    sw.Write("<html><body>Hello There!</body></html>");
                    sw.Flush();
                }
            }
            // Close connection 
            client.Close();
        }
    }
    catch (SocketException e)
    {
        Console.WriteLine("SocketException: {0}", e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.