My HttpListener for some reason can't listen to prefix http://127.0.0.100:50001/, getting an error
Unhandled exception. System.Net.HttpListenerException (32): The process cannot access the file because it is being used by another process.
at System.Net.HttpListener.AddPrefixCore(String registeredPrefix)
at System.Net.HttpListener.AddAllPrefixes()
at System.Net.HttpListener.Start()
at Program.Main(String[] args) in C:\Users\krs\Documents\Testbed\dotnet\Program.cs:line 11
This error usually means that the port is already in use, so I go and check with different utilities:
TCPView:
TCPView no 50001 port usage
netstat:
netstat no 50001 port usage
cports:
cports no 50001 port usage
There is no mention of this port nowhere. But, on my laptop, with the same build of Windows 10 it works. And it works on the machines of my coworkers. It is only my computer that has this issue and I cannot figure out why :(
Minimal C# example:
static void Main(string args)
{
var Listener = new HttpListener();
Listener.Prefixes.Add("http://127.0.0.100:50001/");
Listener.Start();
Console.WriteLine("Ok!");
}
Thank you to @shingo’s comment for providing the link that explains the issue!
According to one of the commenters:
The root cause of the HttpListenerException (32) on ports 50000–50059 is that HTTP.sys (the kernel-mode listener underpinning HttpListener) cannot bind these ports because Windows has them reserved as part of its dynamic (ephemeral) port range or via excluded port ranges on the local machine. By default on modern Windows (Vista and later), TCP ephemeral ports span 49152 to 65535, and attempting to bind within that range without first excluding or reconfiguring it leads to “Error 32: The process cannot access the file because it is being used by another process.”
To fix this you can adjust the dynamic port range:
netsh int ipv4 set dynamic tcp start=60000 num=5536
Or exclude specific ports:
netsh int ipv4 add excludedportrange protocol=tcp startport=50000 numberofports=10
See the link for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With