Skip to content Skip to sidebar Skip to footer

"[errno 10048] Only One Usage Of Each Socket Address (protocol/network Address/port) Is Normally Permitted" After Closing And Reopening Python Socket

I have this code: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.bind(('10.0.0.253', 8080)) except: s.close() s = socket.socket(socket.AF_INET, socket.SOC

Solution 1:

I can simulate your problem by running the python script twice concurrently. The problem is that some other application is currently also using the socket.

In your code, the except block is triggered when your socket fails to bind. However, when you try to .close() the socket and try again, it does nothing as it is trying to .close() an unbound socket. Hence, your code is equivalent to:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('10.0.0.253', 8080))

s.listen(1)
conn, addr = s.accept()

Note that when you .close() the socket, it also does not .close() the currently bound application, it attempts to .close() to socket object you created. So, trying to bind the socket again will fail.

To find out what is using the socket, you can use netstat:

netstat -aon | findstr :8080

This should give you something like:

TCP127.0.0.1:80800.0.0.0:0LISTENING6604

Then, if you want to kill it forcefully, you can use TaskKill and the application's PID:

TaskKill /PID 6604/F

Post a Comment for ""[errno 10048] Only One Usage Of Each Socket Address (protocol/network Address/port) Is Normally Permitted" After Closing And Reopening Python Socket"