最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sockets - Debian Server with SO_REUSEADDR on Python - Stack Overflow

programmeradmin24浏览0评论

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

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

Share Improve this question asked Dec 29, 2024 at 16:22 Peter_Brown_USAPeter_Brown_USA 417 bronze badges 7
  • Why on earth are you developing as root? – Gerald Schneider Commented Dec 29, 2024 at 17:09
  • If you are still getting Address already in use after 2 minutes between invocations, then perhaps there is something still running on that port. ss -ptn dport = :1712 should tell you what is occupying it. – Bob Goddard Commented Dec 29, 2024 at 17:29
  • Gerald, this is a cheap KVM server and the default setup is for root – Peter_Brown_USA Commented Dec 29, 2024 at 18:26
  • Bob, I ran your command and there is nothing in use, so I ran my script and Ctrl-Z out and ran the command again (still nothing) then ran my script again which produced the error and then ran the command again and still no ports. See next comment for output... – Peter_Brown_USA Commented Dec 29, 2024 at 18:29
  • Part 1... root@9b6f7:~# ss -ptn dport = :1712 State Recv-Q Send-Q Local Address:Port Peer Address:Port Process root@9b6f7:~# python3 w1.py Socket successfully created socket binded to 1712 socket is listening Got connection from ^Z [1]+ Stopped python3 w1.py root@9b6f7:~# ss -ptn dport = :1712 State Recv-Q Send-Q Local Address:Port Peer Address:Port Process 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 – Peter_Brown_USA Commented Dec 29, 2024 at 18:34
 |  Show 2 more comments

1 Answer 1

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论