0

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).

  • 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 Apr 17 '20 at 15:18
  • net stop WlanSvc & net start WlanSvc seems to free the ports. Why? – bers Apr 22 '20 at 15:44
  • Not all that surprisingly, this approach has stopped working after upgrading to Windows 10, 2004. – bers Jun 3 '20 at 9:05
  • Related: stackoverflow.com/questions/54010365/… – bers Sep 4 '20 at 9:48
  • Related: stackoverflow.com/questions/54217076/… – bers Sep 4 '20 at 9:49
1

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.

  • Great information. netsh int ip show excludedportrange protocol=tcp shows the port ranges in questions - they are not in use, but excluded. – bers Sep 4 '20 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.