排查MinGW64编译程序无法绑定特定端口的原因
Let's break down why you're hitting the bind failed with error: 10013 (WSAEACCES) issue, and how to diagnose and fix it:
Root Cause
The problem almost certainly stems from Windows TCP Port Exclusion Ranges. Windows automatically reserves blocks of ports for system services like Hyper-V, WSL, Windows Update, or internal processes—even if those services aren't running. These ports are locked off from user-mode applications, which explains why your program can bind to 27015 but not 9010 or other problematic ranges, even when netstat shows they're unused.
Verify if Ports Are Excluded
To confirm this, open an elevated Command Prompt (run as administrator) and run:
netsh int ipv4 show excludedportrange protocol=tcp
Check if your target ports (e.g., 9010, 8820-9519) fall within any of the listed Start Port/End Port ranges. If yes, that's exactly why bind is failing.
You should also check IPv6 exclusions in case your program might try to use IPv6:
netsh int ipv6 show excludedportrange protocol=tcp
Solutions to Fix the Binding Issue
1. Run the Program as Administrator
Some excluded ports allow binding by elevated (admin) processes. Try launching test.exe via right-click → "Run as administrator", or execute it from an elevated Command Prompt. This might bypass the restriction for certain reserved ports.
2. Adjust Windows' Dynamic Port Range
Windows uses a default dynamic port range (usually 49152-65535) for outgoing connections, but sometimes system exclusions bleed into lower ranges you want to use. You can reconfigure this range to avoid your desired ports:
# First, check the current dynamic port range netsh int ipv4 show dynamicport tcp # Modify the range (example: set it to 50000-65535) netsh int ipv4 set dynamicport tcp start=50000 num=15536
Reboot your PC after making this change. This will prevent Windows from reserving ports in your target range for dynamic use.
3. Delete Specific Port Exclusion Rules
If you're certain a port block is being unnecessarily reserved, you can remove the exclusion rule (admin rights required):
# Example: Remove exclusion for 8820-9519 (699 ports total) netsh int ipv4 delete excludedportrange protocol=tcp startport=8820 numberofports=699
⚠️ Note: This could break system services that depend on those ports, so only do this if you're sure they're not in use by critical processes.
4. Use Ports Outside Excluded Ranges
The simplest fix is to stick with ports that aren't in the exclusion list—like your working 27015 port. Use the netsh command above to identify safe port ranges for your application.
Extra Tools for Verification
- Resource Monitor: Open Task Manager → Performance tab → Open Resource Monitor → Network tab. Check the "Listening Ports" section to confirm no process is using your target port (though in your case, it's likely a system reservation, not a process).
- PowerShell: Use this command to check port usage more clearly than
netstat:Get-NetTCPConnection -LocalPort 9010
内容的提问来源于stack exchange,提问作者sdbbs




