保留端口占用问题

开发中如果遇到netstat -ano查不到占用但应用一直提示被占用,很可能是hyper-v的保留端口问题引起,按照如下方式排查即可。

查看哪些端口被保留了

  1. Windows+x选择管理员的powershell然后输入netsh interface ipv4 show excludedportrange protocol=tcp 确定执行,列出类似如下列表

``` 协议 tcp 端口排除范围

开始端口 结束端口 ---------- -------- 5357 5357 7918 8017 8018 8117 8118 8217 8308 8407 8408 8507 8559 8658 8681 8780 8872 8971 8972 9071 50000 50059 *

    • 管理的端口排除。 ```
  • 上述端口都被hyper-v申明为保留端口了。

解决办法

  1. 使用上述之外的端口;
  2. 不让hyper-v保留指定端口或范围
  3. 关闭hyper-v,power shell执行dism.exe /Online /Disable-Feature:Microsoft-Hyper-V
  4. 设置可用的动态端口范围(给hyper-v的)netsh int ipv4 set dynamicport tcp start=30000 num=16383
  5. 排除动态端口范围netsh int ipv4 add excludedportrange protocol=tcp startport=8000 numberofports=500