Search code examples
pythonsockets

"[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.SOCK_STREAM)
    s.bind(('10.0.0.253', 8080))


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

which binds, then if it encounters an error with that, destroys the socket then creates a new one and binds it to the same IP and port. For some reason, even after closing the socket, I get this error:

Traceback (most recent call last):
File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 68, in <module>
  main()
  File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 66, in main
  connect()
File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 43, in connect
  s.bind(('10.0.0.253', 8080))
File "C:\Python27\lib\socket.py", line 228, in meth
  return getattr(self._sock,name)(*args)
error: [Errno 10048] Only one usage of each socket address (protocol/network 
address/port) is normally permitted

The only solution I've found is to use s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1), but when I try that I get this error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions. I

I am on Windows 10, Python 2.7.13.


Solution

  • 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:

    TCP    127.0.0.1:8080         0.0.0.0:0              LISTENING       6604
    

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

    TaskKill /PID 6604 /F