如何在 C# 控制台应用程序中设置 Web 服务器

问题描述 投票:0回答:2

这是代码,问题如下:

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

namespace BasicWebServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var ipAddress = IPAddress.Parse("127.0.0.1");
            var port = 1043;
            var serverListener = new TcpListener(ipAddress, port);

            serverListener.Start();

            Console.WriteLine($"Server started on port {port}.");
            Console.WriteLine("Listening for requests...");

            while (true)
            {    var connection = serverListener.AcceptTcpClient();

                var networkStream = connection.GetStream();

                var content = "Hello World!";

                var contentLength = Encoding.UTF8.GetByteCount(content);

                var response = $@"HTTP/1.1 200 OK
Content-Type: text/plain;
charset=UTF-8
Content-Length:{contentLength}
{content}";

                var responseBytes = Encoding.UTF8.GetBytes(response);

                networkStream.Write(responseBytes, 0, responseBytes.Length);

                connection.Close();
            }

        }
        
    }
}

应该监听该端口的传入连接并显示数据。我得到的第一个错误是:

System.Net.Sockets.SocketException: '尝试访问 套接字以其访问权限禁止的方式'

我尝试查看命令提示符以查看哪些端口被排除,但它不在该列表中。我使用的命令是

netsh 接口 ipv4 显示排除的端口范围协议=tcp

然后我检查是否可以通过此命令在 PowerShell 中连接到它

测试网络连接-计算机名本地主机-端口1043

结果是这样的

  • 计算机名称:本地主机
  • 远程地址:::1
  • 远程端口:1043
  • 接口别名:环回伪接口 1
  • 来源地址:::1
  • TcpTestSucceeded:正确

我还尝试禁用防火墙以查看它是否阻止了该端口,但什么也没发现。

那么如何检查我可以使用哪些端口而不出现此错误?

我遇到的第二个错误是

c# exception webserver console-application port
2个回答
4
投票

这是更新后的代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace BasicWebServer.Server
{
    public class HttpServer
    {
        private readonly IPAddress ipAddress;
        private readonly int port;
        private readonly TcpListener serverListenter;

        public HttpServer(string ipAddress, int port)
        {
            this.ipAddress = IPAddress.Parse(ipAddress);
            this.port = port;

            this.serverListenter = new TcpListener(this.ipAddress, port);
        }

        public void Start()
        {
            Console.WriteLine($"Server started on port {port}.");
            Console.WriteLine("Listening for requests...");

            while (true)
            {
                this.serverListenter.Start();

                var connection = serverListenter.AcceptTcpClient();

                var networkStream = connection.GetStream();

                WriteResponse(networkStream, "Hello there!");

                connection.Close();
            }
        }

        private void WriteResponse(NetworkStream networkStream, string message)
        {
            var contentLength = Encoding.UTF8.GetByteCount(message);

            var response = $@"HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: {contentLength}

{message}";
            var responseBytes = Encoding.UTF8.GetBytes(response);

            networkStream.Write(responseBytes, 0, responseBytes.Length);
        }
    }
}

-1
投票

您可以使用此库,它可以帮助您为控制台或 Windows 应用程序创建独立的 HTTP 服务器https://www.nuget.org/packages/SharpHttpServer

© www.soinside.com 2019 - 2024. All rights reserved.