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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:02:01+00:00 2026-05-21T14:02:01+00:00

I am trying to implement a simple http csharp server which sends a document

  • 0

I am trying to implement a simple http csharp server which sends a document such as index.html to a the browser when the user connects by connecting via the server IP. I am currently a little confused about how i would go about sending a simple .html document over the web browser so the connecting user can see it.

The outstream is where all the information is being sent to the browser.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace TCP_Socket
{
    class ConnectionThread
    {
        public ConnectionThread(Socket socketToHandleConnection)
        {
            connection = socketToHandleConnection;
        }

        Socket        connection       = null;   //TCP/IP socket to handle the actual connection
        NetworkStream connectionStream = null;
        BinaryReader  inStream         = null;
        BinaryWriter  outStream        = null;
        String        userName         = null;

        public void run()
        {
            connectionStream = new NetworkStream(connection);

            inStream  = new BinaryReader(connectionStream);
            outStream = new BinaryWriter(connectionStream);

            userName = Environment.UserName;

            byte b = 0;
            String s = "";      //This will contain all the stuff the browser transmitted,
                                //but "all in one go".
            try
            {
                while (connectionStream.DataAvailable)
                {
                    b = (byte)inStream.ReadSByte();
                    Console.Out.Write((char)b);
                    s += (char)b;
                }

                String[] items = s.Split();//This will contain all the stuff the browser transmitted,


            }
            catch (EndOfStreamException eos)
            {
                Console.Out.WriteLine("Unexpected end of stream.");
                Console.Out.WriteLine("Error caused by " + eos.Message);
            }
            Console.Out.WriteLine("End of stream.");

            String stringOut = "HTTP/ 1.1 200 OK\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "Content-Type: text/html\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "Date: ";
            outStream.Write(stringOut.ToCharArray());
            stringOut = Convert.ToString(DateTime.Now);
            outStream.Write(stringOut.ToCharArray());   
            stringOut = "\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "<html>\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "<body>\r\n";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "Welcome to <strong>" + userName + "'s </strong>primative HTTP server";
            outStream.Write(stringOut.ToCharArray());

            stringOut = "</body></html>\r\n";
            outStream.Write(stringOut.ToCharArray());

            inStream.Close();
            outStream.Flush();
            outStream.Close();
            connectionStream.Close();
            connection.Close();

            Console.Out.WriteLine("Done; Connection closed.");
        }
    }
}
  • 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-21T14:02:02+00:00Added an answer on May 21, 2026 at 2:02 pm

    You dont need to write all of HTTP code using sockets as HttpListener is already provided from .NET 2.0. give it a try if it fits your requirements.

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

Sidebar

Related Questions

I am trying to implement a simple servlet which uses a HTTP session in
im trying to implement simple secured client server communiction using WCF. when im launching
As part of learning ruby/rails, I'm trying to implement http://github.com/professionalnerd/simple-private-messages into my application from
i am trying to implement a simple server application in java. all it does
I'm new to Flex SDK and trying to implement a simple project using Doug
I am trying to implement simple chat application using flex. In it all my
I'm trying to implement a simple watermark on a text box that disappears when
I am trying to implement a simple authorization strategy for my Wicket application. I
I am trying to implement a simple widget for display on the home screen.
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).

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.