Reputation: 77
Thank you for reading this, I appreciate any help!
I don't really seem to find an answer that satisfies the following questions, mostly explained unclearly. Imagine I'd create a socket object in Python:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Then, I'd like to set the options of that socket object (server), with the following three arguments.
socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
I'm kind of confused by these arguments. Firstly, the SOL_SOCKET, is it some kind of constant value that actually allows the following arguments in the signature (like reuseaddr) to implement it on the socket level? (More info is welcome)
Secondly the REUSEADDR what does it actually do? It allows the server to reuse (accept connections) the same ip and port, while it's in close-wait or time-wait state. If that's correct, I don't seem to get why that is needed, can't I just keep accepting connections on the same port and ip, without using it, isn't that setting automatically used, it would be my best guess that you can have multiple connections on a single port and ip address without using that argument?
Finally, what does the 1
mean at the end?
The primary reason I'm asking this question because I thought if I wouldn't use REUSEADDR that I could still accept other connections on the same port and ip
Thank you for the help, have a great day!
Upvotes: 4
Views: 9204
Reputation: 598448
Firstly, the SOL_SOCKET, is it some kind of constant value that actually allows the following arguments in the signature (like reuseaddr) to implement it on the socket level?
Yes. setsockopt()
options are organized in groups identified by levels. There are socket-level options, IP-level options, TCP-level options, etc. SO_REUSEADDR
(and SO_REUSEPORT
) is a socket-level option, as it affects the socket object itself (when it is binding to a local IP/port pair).
Secondly the REUSEADDR what does it actually do?
This is well-documented on most platforms. Python sockets are just a thin wrapper around platform BSD-style sockets.
It allows the server to reuse (accept connections) the same ip and port, while it's in close-wait or time-wait state.
It has nothing to do with accepting connections. It has only to do with being able to bind()
a new socket to a local IP/port pair after a previous socket has stopped using that same pair.
If that's correct, I don't seem to get why that is needed
Because a local IP/port pair can't normally be reused for a new socket binding while the pair is in the CLOSE_WAIT or TIME_WAIT state. The whole purpose of those states is to wait a period of time for pending data to be flushed for a previous communication. By allowing a new socket to re-use the IP/port pair while data is still pending, a new socket can potentially read data from a previous conversation. So, SO_REUSEADDR
is disabled by default. But this is not really a problem for TCP server sockets (more so for UDP sockets), so SO_REUSEADDR
is commonly used to allow rapid reuse of the IP/port pair after closing a server and restarting it.
can't I just keep accepting connections on the same port and ip, without using it, isn't that setting automatically used
If your listening TCP socket stays active, yes. SO_REUSEADDR
has nothing to do with a bound listening socket's ability to accept client connections.
it would be my best guess that you can have multiple connections on a single port and ip address without using that argument?
Once a listening socket has been successfully bound to the IP/port pair, yes.
Finally, what does the
1
mean at the end?
SO_REUSEADDR
is a boolean option. It only has two defined values, 0 (off) and 1 (on).
The primary reason I'm asking this question because I thought if I wouldn't use REUSEADDR that I could still accept other connections on the same port and ip
As long as your listening socket is active, yes. But if you close your listening socket and create a new one, it has to be re-bound to a local IP/port before it can start accepting connections, and that IP/port might not be ready for re-use yet, unless SO_REUSEADDR
is enabled.
Upvotes: 6