Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单C#Http/TCP服务器_C#_Http_Sockets_Tcp - Fatal编程技术网

简单C#Http/TCP服务器

简单C#Http/TCP服务器,c#,http,sockets,tcp,C#,Http,Sockets,Tcp,我试图实现一个简单的http csharp服务器,当用户通过服务器IP连接时,它会将index.html之类的文档发送到浏览器。我现在对如何通过web浏览器发送一个简单的.html文档以便连接的用户可以看到它感到有点困惑 扩展流是将所有信息发送到浏览器的地方 using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.IO; using Syst

我试图实现一个简单的http csharp服务器,当用户通过服务器IP连接时,它会将index.html之类的文档发送到浏览器。我现在对如何通过web浏览器发送一个简单的.html文档以便连接的用户可以看到它感到有点困惑

扩展流是将所有信息发送到浏览器的地方

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.");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Net.Sockets;
使用System.IO;
使用系统线程;
命名空间TCP_套接字
{
类连接读取
{
公共连接读卡器(插座插座与手持连接)
{
连接=插座与连接器连接;
}
Socket connection=null;//处理实际连接的TCP/IP套接字
NetworkStream connectionStream=null;
BinaryReader inStream=null;
BinaryWriter超流=空;
字符串userName=null;
公开募捐
{
connectionStream=新的网络流(连接);
inStream=新的二进制读取器(connectionStream);
扩展流=新的二进制编写器(connectionStream);
用户名=Environment.userName;
字节b=0;
字符串s=”“;//这将包含浏览器传输的所有内容,
//但是“一气呵成”。
尝试
{
while(connectionStream.DataAvailable)
{
b=(字节)inStream.ReadSByte();
Console.Out.Write((char)b);
s+=(char)b;
}
String[]items=s.Split();//这将包含浏览器传输的所有内容,
}
捕获(EndOfStreamException eos)
{
Console.Out.WriteLine(“意外的流结束”);
Console.Out.WriteLine(“由“+eos.Message”引起的错误);
}
Console.Out.WriteLine(“流结束”);
String stringOut=“HTTP/1.1 200正常\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“内容类型:text/html\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“日期:”;
outStream.Write(stringOut.ToCharArray());
stringOut=Convert.ToString(DateTime.Now);
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
stringOut=“欢迎使用”+userName+“'s灵长类HTTP服务器”;
outStream.Write(stringOut.ToCharArray());
stringOut=“\r\n”;
outStream.Write(stringOut.ToCharArray());
流内关闭();
冲水;
exptream.Close();
connectionStream.Close();
connection.Close();
Console.Out.WriteLine(“完成;连接关闭”);
}
}
}

您不需要像.NET 2.0提供的那样使用套接字编写所有HTTP代码。如果它符合您的要求,请尝试一下。

搜索后,我找到了一些其他解决方案:

其他类似的嵌入式服务器包括:


HttpListener的缺点是,在允许您接受连接之前,您必须在操作系统中运行一些东西,即netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\users有其他解决方案吗?我在使用HttpListenerHttpListener时遇到一些问题,在windows server 2019中也无法使用。它不能仅由本地主机通过lan访问。