4

I am using this Python code to listen to a port on my Windows 10 system:

import socket
for port in (50059, 50060, 50959, 50960):
  try:
    print(socket.socket(socket.AF_INET, socket.SOCK_STREAM).bind(('localhost', port)))
  except OSError as e:
    print(e)

However, it prints

None
[WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
[WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
None

meaning that I cannot access ports 50060 and 50959. I experimented further and confirmed that I cannot listen to any port in the range 50060-50959.

Update: After upgrading to Windows 10, 2004, it's port range 50060-59708 that is blocked somehow.

I would like to listen to port 50323, which falls into this range (in both cases).

I have tried netstat -aonq to find the culprit, but without success

C:\Users\bers>netstat -aon | grep :50
  TCP    0.0.0.0:5040           0.0.0.0:0              LISTENING       8008 (Connected Devices Platform Service)
  TCP    10.0.0.20:50985        51.105.249.223:443     ESTABLISHED     5208 (Windows Push Notifications System Service)
  UDP    0.0.0.0:500            *:*                                    4936
  UDP    0.0.0.0:5050           *:*                                    8008
  UDP    [::]:500               *:*                                    4936

Nothing else seems to be listening in this range. So why can't I? 50060-50959 is exactly 900 ports - I am sure this is no coincidence. (Update: Well, I was - it's 9649 ports on Windows 10, 2004.)

I have checked

This means I have also tried netstat -q, see above, tcpview.exe, resmon.exe, and disabled Internet Connection Sharing (ICS).

5
  • For now, I can add that shutting down Workstation, WWAN Autoconfig, and WLAN Autoconfig services fixed the problem until the next reboot. Even restarting the services does not make the problem re-appear. Will do more bisecting later. This does not answer the question how to immediately identify the culprit, though.
    – bers
    Commented Apr 17, 2020 at 15:18
  • net stop WlanSvc & net start WlanSvc seems to free the ports. Why?
    – bers
    Commented Apr 22, 2020 at 15:44
  • Not all that surprisingly, this approach has stopped working after upgrading to Windows 10, 2004.
    – bers
    Commented Jun 3, 2020 at 9:05
  • Related: stackoverflow.com/questions/54010365/…
    – bers
    Commented Sep 4, 2020 at 9:48
  • Related: stackoverflow.com/questions/54217076/…
    – bers
    Commented Sep 4, 2020 at 9:49

1 Answer 1

4

If you face this issue on Win10 2004 that's because of an issue in this update, do the following:

netsh int ipv[46] set dynamic tcp start=49152 num=16384

reg add HKLM\SYSTEM\CurrentControlSet\Services\hns\State /v EnableExcludedPortRange /d 0 /f

I face this on opening Jetbrains IDEs and many other program that use sockets.

3
  • 1
    Great information. netsh int ip show excludedportrange protocol=tcp shows the port ranges in questions - they are not in use, but excluded.
    – bers
    Commented Sep 4, 2020 at 9:48
  • In my case, only the netsh command was necessary (followed by a restart); so I'd recommend only running the registry-changing command if the netsh command is insufficient.
    – Venryx
    Commented Aug 27, 2021 at 1:36
  • @Venryx the commands "net stop winnat" and then "net start winnat" should do the trick, without having to do a restart
    – zentrunix
    Commented Sep 9, 2022 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .