Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 335f204

Browse files
committed
#12781: Mention SO_REUSEADDR flag near socket examples
1 parent d86ac4c commit 335f204

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Doc/library/socket.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,25 @@ the interface::
10141014
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
10151015

10161016

1017+
Running an example several times with too small delay between executions, could
1018+
lead to this error::
1019+
1020+
socket.error: [Errno 98] Address already in use
1021+
1022+
This is because the previous execution has left the socket in a ``TIME_WAIT``
1023+
state, and can't be immediately reused.
1024+
1025+
There is a :mod:`socket` flag to set, in order to prevent this,
1026+
:data:`socket.SO_REUSEADDR`::
1027+
1028+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1029+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1030+
s.bind((HOST, PORT))
1031+
1032+
the :data:`SO_REUSEADDR` flag tells the kernel to reuse a local socket in
1033+
``TIME_WAIT`` state, without waiting for its natural timeout to expire.
1034+
1035+
10171036
.. seealso::
10181037

10191038
For an introduction to socket programming (in C), see the following papers:

0 commit comments

Comments
 (0)