image On automation image Modify YML files with PowerShell



Forbidden access to a socket on Windows and how to solve it

Roadblock

Lately, I was getting unfamiliar errors when running .NET applications or spawning docker containers on Windows. I was really annoyed I could not run the application on port 5001 from Visual Studio since it was working fine just a day before.

The core of the message I got was An attempt was made to access a socket in a way forbidden by its access permissions.

“Which application is using this port?” I did a “good old” computer restart … and … nothing.

I run a netstat -ano which lists all computer connections and ports, and the port was not there. “Huh?”

After a quick Google search, I found a command, that can show me “reserved” or excluded ports on my Windows instance.

The command is netsh interface ipv4 show excludedportrange protocol=tcp:

“Aha, there you are! How come you are suddenly reserved?” Line 10 shows a reserved port range which includes my bellowed port! I’ve used this port for ages and never had any issues. It looks like some update or upgrade to the system or application did that. After a few more Google searches and “strength training with my coffee mug” I found a promising post on Github. As you’ll soon find out the culprit was the Hyper-V which for some reason reserved the ports for itself. I suspect a Docker for Windows update was the reason in this case.

Anyway, below is a compiled solution to my problem.

Solution

Open the CMD or Powershell as administrator and run the commands below.

1. Disable Hyper-V

First disable the Hyper-V and restart a computer:

dism.exe /Online /Disable-Feature:Microsoft-Hyper-V

2. Exclude the ports from the list of available ports that can be reserved (by Hyper-V in my case)

Then if you run the netsh interface ipv4 show excludedportrange protocol=tcp again you’ll see different results from before. Hyper-V was disabled and the ports were released. Awesome! Let’s exclude the ports. The command below will exclude 10 consecutive ports starting from 5000.

netsh int ipv4 add excludedportrange protocol=tcp startport=5000 numberofports=10

What if I get the error The process cannot access the file because it is being used by another process?

Adjust the command to exclude a smaller range of ports. One of your applications is using one or more of those 10 ports that you would like to exclude.

3. Enable Hyper-V

Now let’s enable the Hyper-V and restart!

dism.exe /Online /Enable-Feature:Microsoft-Hyper-V /All

4. Check if port is excluded

Run the netsh interface ipv4 show excludedportrange protocol=tcp again:

Yes! I can run my application on port 5001 again! “WOOOHOOOO” :)!

About Miha
I've always been fascinated by computers. I am happy that I can work in this creative field of software development, where I can solve problems. I am also an avid cyclist :) and I love to learn...a lot!
Related Posts
  • All
  • By Author
  • By Category
  • By Tag

Leave a Reply