diff options
| author | Sandro Tosi <sandro.tosi@gmail.com> | 2011-09-02 18:04:20 (GMT) |
|---|---|---|
| committer | Sandro Tosi <sandro.tosi@gmail.com> | 2011-09-02 18:04:20 (GMT) |
| commit | a95d19efa62645adc1adb2c7cd9861a1856b41c2 (patch) | |
| tree | 826cbd57b0c476822270add1cfd2b6eec478a344 | |
| parent | b71c15d407bdcbb314c71f1174a3db33875f49a9 (diff) | |
| download | cpython-a95d19efa62645adc1adb2c7cd9861a1856b41c2.zip cpython-a95d19efa62645adc1adb2c7cd9861a1856b41c2.tar.gz cpython-a95d19efa62645adc1adb2c7cd9861a1856b41c2.tar.bz2 | |
#12781: Mention SO_REUSEADDR flag near socket examples
| -rw-r--r-- | Doc/library/socket.rst | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index c5010bb..3fce587 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -989,3 +989,22 @@ the interface:: # disabled promiscuous mode s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) + + +Running an example several times with too small delay between executions, could +lead to this error:: + + socket.error: [Errno 98] Address already in use + +This is because the previous execution has left the socket in a ``TIME_WAIT`` +state, and can't be immediately reused. + +There is a :mod:`socket` flag to set, in order to prevent this, +:data:`socket.SO_REUSEADDR`:: + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind((HOST, PORT)) + +the :data:`SO_REUSEADDR` flag tells the kernel to reuse a local socket in +``TIME_WAIT`` state, without waiting for its natural timeout to expire. |
