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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:42:03+00:00 2026-05-13T10:42:03+00:00

how can i send message back to the browser or localhost for example if

  • 0

how can i send message back to the browser or localhost for example if i want to display message called…. This is test in browser.

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpListener
{
    public static void Main()
    {


     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..");

                 }

                // 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-13T10:42:04+00:00Added an answer on May 13, 2026 at 10:42 am

    Instead of reading until you retrieve 0 bytes, you should read until you find a blank line. Calling Read() on the last bit of the open network connection just blocks until the browser sends more data (which it won’t because it’s sent everything and is waiting on you).

    class MyTcpListener
    {
        public static void Main()
        {
    
    
            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();
    
                // 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!");
    
    
                    // Get a stream object for reading and writing
                    using(NetworkStream stream = client.GetStream())
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        List<byte> msg = new List<byte>();
                        // Loop to receive all the data sent by the client.
                        string data;
                        while ((data = sr.ReadLine()) != "")
                        {
                            Console.WriteLine(String.Format("Received: {0}", data));
    
                            // Process the data sent by the client.
                            data = data.ToUpper();
    
                            msg.AddRange(System.Text.Encoding.ASCII.GetBytes(data));
    
                        }
    
                        // Send back a response.
                        stream.Write(msg.ToArray(), 0, msg.Count);
                        Console.WriteLine("Sending message..");
                    }
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
    
            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 316k
  • Answers 316k
  • 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 Visual Studio locks the source code in debug mode. So… May 13, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer For "efficiency", don't have two versions of the entity; just… May 13, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer Have you considere partitioning? Improving Database Performance with Partitioning. May 13, 2026 at 11:27 pm

Related Questions

When I have a markup error in my XHTML page, Mozilla Firefox displays the
I modified an existing form and saved it on my desktop as .oft file.
By default, Apache 2.0.52 will respond to any HTTP TRACE request that it receives.
I have a server application receiving messages from a JMS queue. And client applications
If I am to send a message to a mobile device from a website

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.