DEV Community

Danny Reed
Danny Reed

Posted on

An attempt was made to access a socket...

The Error

Today I tried to fire up a project I haven't run in a long time. It's a web app, and it happens to launch on a specific port: 50617.

I was frustrated to see this:

An attempt was made to access a socket in a way forbidden by its access permissions.

The Reason

I use Docker on Windows with the WSL2 engine, which involves Hyper-V. Some quick googling revealed that this error usually comes up when you're trying to access a port that has been reserved by something else. Hyper-V reserves a bunch of ephemeral ports, so it's a common culprit.

To confirm your port has already been reserved, open an admin command prompt and run this:

netsh interface ipv4 show excludedportrange protocol=tcp
Enter fullscreen mode Exit fullscreen mode

This will show you the ranges that have been reserved. My port was in one of the ranges.

The Soltuion

I found the solution on this GitHub comment, so full props to the author for finding the solution. I'm just writing this post to help make this easier to find than digging through long GitHub comment chains :)

The gist of it is:

  1. Open an Administrator command prompt
  2. Stop the Windows NAT Driver service

    net stop winnat
    
  3. Create a manual reservation for the specific port you're trying to use (50617 in my case).

    netsh int ipv4 add excludedportrange protocol=tcp startport=50617 numberofports=1
    
  4. Start the Windows NAT Driver service

    net start winnat
    
  5. Run your project!

Conclusion

This should get your port reserved and let you get your project running. If you have additional tips related to this issue, leave a comment!

Thanks!

Top comments (0)