简单的C# Http/TCP服务器

4

我正在尝试实现一个简单的C# HTTP服务器,当用户通过服务器IP连接时,向浏览器发送文档,例如index.html。目前,我有些困惑如何将一个简单的.html文档发送到Web浏览器,以便连接的用户可以看到它。

输出流(outstream)是将所有信息发送给浏览器的地方。

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.");
        }
    }
}
2个回答

2

你不需要使用套接字编写所有HTTP代码,因为.NET 2.0已经提供了HttpListener。如果符合你的需求,请尝试使用它。


4
HttpListener的缺点是,在接受连接之前,您必须在操作系统中运行一些东西,即运行"netsh http add urlacl url = http://+:80/MyUri user=DOMAIN\user"命令。 - Vdex
1
有其他的解决方案吗?我在使用HttpListener时遇到了一些问题。 - Mahdi
HttpListner在Windows Server 2019中也无法使用。它只能通过localhost访问,无法通过局域网访问。 - Tommix


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接