【问题标题】:Unable to listen on specific Windows 10 ports无法侦听特定的 Windows 10 端口
【发布时间】:2020-02-13 21:46:54
【问题描述】:

我发现我的 Windows 10 机器上有许多端口(1)没有被任何进程使用,(2)我无法监听。

我在尝试运行使用端口 3000 的节点服务器时发现了这个问题。我发现了很多关于这个主题的问题。这个比较典型:Node.js Port 3000 already in use but it actually isn't?

这个问题和类似问题的所有受访者都建议使用“netstat -ano”来查找正在使用该端口的进程并将其杀死。

我发现有大量与进程无关的端口被阻塞。这与 AV 或防火墙无关。我关闭了防火墙,我只有 Windows Defender AV。

我编写了一个程序来监听 127.0.0.1 上 3000 到 5000 之间的端口。

        int port = 3000;
        while(port <= 5001)
        {
            try
            {
                ListenOnPort(port);
                ++port;

            }
            catch (Exception ex)
            {
                Console.WriteLine($"Listen on {port} failed: {ex.Message}");
                ++port;
            }
        }

ListenOnPort 在哪里...

    private static void ListenOnPort(int v)
    {
        var uri = new UriBuilder("http", "127.0.0.1", v);
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add(uri.Uri.ToString());
        Console.WriteLine($"Listening on {v}");
        listener.TimeoutManager.IdleConnection = new TimeSpan(0, 0, 1);
        listener.Start();
        var task = listener.GetContextAsync();
        if(task.Wait(new TimeSpan(0,0,1)))
        {
            HttpListenerResponse response = task.Result.Response;
            // Construct a response.
            string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            // Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            // You must close the output stream.
            output.Close();
        }
        listener.Stop();
    }

程序产生了类似这样的输出...

Listening on 3000
Listen on 3000 failed: The process cannot access the file because it is being used by another process
Listening on 3001
Listen on 3001 failed: The process cannot access the file because it is being used by another process
Listening on 3002
Listen on 3002 failed: The process cannot access the file because it is being used by another process
Listening on 3003
Listen on 3003 failed: The process cannot access the file because it is being used by another     process
Listening on 3004
Listen on 3004 failed: The process cannot access the file because it is being used by another process
Listening on 3005
Listen on 3005 failed: The process cannot access the file because it is being used by another process
Listening on 3006
Listening on 3007
Listening on 3008
Listening on 3009
Listening on 3010

我发现在 3000 和 5000 之间,有 624 个端口被阻塞。同时“netstat -ano”显示该范围内正好有 5 个端口在使用。那么是什么阻塞了其他 619 端口呢?

【问题讨论】:

    标签: windows http windows-10 port


    【解决方案1】:

    对...

    在寻找其他东西时,我找到了答案(至少是问题的根源)。我无法连接到这些端口的原因是它们都是 Windows 上排除的端口范围的一部分。要查看排除的端口,请使用...

    $ netsh int ipv4 show excludedportrange tcp
    

    还有一个神奇的列表,其中列出了我无法连接的所有端口。这些排除的端口范围显然来自我已经安装的 HyperV 和 Docker。显然有一种方法可以取回端口……这并不容易,因为它涉及卸载 Docker 和 HyperV,然后为自己保留端口范围,然后重新安装 HyperV 和 Docker。不值得。既然我知道如何找到我不能使用的端口,我就不会使用它们!

    【讨论】:

    • 谢谢!我一直想知道为什么我的一些 kubernetes 端口转发在 Windows 10 的浏览器中不起作用,但在 WSL2 中起作用。例如。 ` Elasticsearch = 9200 Prometheus = 9090 Alertmanager = 9093 PushGateway = 9091 ` 我可以成功创建端口转发,但在 WSL2 之外,Windows 系统并没有监听这些端口。现在我知道为什么了。
    • 谢谢~!。我在监听端口为 3306 的 mariadb 上遇到了同样的问题。
    猜你喜欢
    • 2021-10-10
    • 2019-05-05
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2020-04-03
    • 2021-12-16
    • 2014-08-03
    • 2019-08-16
    相关资源
    最近更新 更多