无法在特定的Windows 10端口上侦听

无法在特定的Windows 10端口上侦听,windows,http,windows-10,port,Windows,Http,Windows 10,Port,我发现,在我的Windows 10机器上有许多端口(1)没有被任何进程使用,(2)我无法监听 我在运行使用端口3000的节点服务器时发现了这个问题。我发现了很多关于这个话题的问题。这是典型的: 这个问题和类似问题的所有回答者都建议使用“netstat-ano”查找正在使用端口并终止它的进程 我发现有大量被阻塞的端口没有与进程绑定。这与AV或防火墙无关。我关闭了防火墙,只有Windows Defender AV 我写了一个程序,在127.0.0.1上监听3000到5000之间的端口

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

我在运行使用端口3000的节点服务器时发现了这个问题。我发现了很多关于这个话题的问题。这是典型的:

这个问题和类似问题的所有回答者都建议使用“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;
            }
        }
我发现在3000到5000之间,有624个端口被阻塞。同时,“netstat-ano”显示在该范围内使用的端口正好有5个。那么,是什么阻止了619个其他端口呢?

在寻找其他东西时,我找到了答案(至少是问题的根源)。我无法连接到这些端口的原因是,它们都是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系统根本没有监听这些端口。现在我知道为什么了。谢谢!。我在mariadb上遇到了同样的问题,它的侦听端口是3306。
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
$ netsh int ipv4 show excludedportrange tcp