I have written just a simple socket server in Python 3.11.2 on Bookworm Really simple at the moment.
root@9b6f7:~# python3 w1.py
Socket successfully created
Traceback (most recent call last):
File "/root/w1.py", line 7, in <module>
s.bind(('', port))
OSError: [Errno 98] Address already in use
While testing and writing the script I keep getting Address already in use error when running the script and reading up about the error on sockets I see I need to add SO_REUSEADDR
I have added s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) to my script but still keep getting the Address already in use error especially if the script crashes
Here is my simple script that produces the error when running. Could someone help where I might have gone wrong.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print ("Socket successfully created")
port = 1712
s.bind(('', port))
print ("socket binded to %s" %(port))
# put the socket into listening mode
s.listen(5)
print ("socket is listening")
# a forever loop until we interrupt it or an error occurs
while True:
# Establish connection with client.
c, addr = s.accept()
print ('Got connection from', addr )
while True:
data = c.recv(1024)
if not data:
break
c.send('Thank you for connecting'.encode())
c.close()
Thanks
... and Ctrl-Z out
This only suspends the program but does not kill it. The existing listener socket is still there - check with ss -ptnl sport = :1712
(different from the command suggested in the comment). That's why the new bind will fail, even with SO_REUSEADDR. From socket(7):
SO_REUSEADDR
Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. For AF_INET sockets this means that a socket may bind, except when there is an active listening socket bound to the address. ...
Not sure what you want to achieve at the end. But the normal use case for SO_REUSEADDR is to make it possible to bind to the local address even if there is still a not fully closed connection using this local address. This is different from an active listener socket though, which exists in your case. If you want to make sure that this listener socket is gone you need to either close it actively in the process or simply exit the process - but just suspending it with Ctrl-Z is not enough.
If you want instead to have multiple listener sockets on the same local addr at the same time (for example for load balancing) then SO_REUSEPORT should be used.