添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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".
                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.");
                The downside with HttpListener is you have to run something in the OS before it will let you accept connections i.e.  netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\user
– Vdex
                Oct 18, 2016 at 19:36
                HttpListner also no go in windows server 2019. It cant be access via lan only by localhost.
– Tommix
                Aug 28, 2019 at 23:40
  • Sample HTTP Server Skeleton in C#
  • Embedded .NET HTTP Server
  • System.Net HttpListener (as of .NET Framework 3.5 SP1)
  • The HTTP server provided in zeroconf.codeplex.com
  • Simple HTTP Server in C#
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.