mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	#12781: Mention SO_REUSEADDR flag near socket examples
This commit is contained in:
		
							parent
							
								
									d9e1789364
								
							
						
					
					
						commit
						172f374a63
					
				
					 1 changed files with 19 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -1014,6 +1014,25 @@ the interface::
 | 
			
		|||
   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.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. seealso::
 | 
			
		||||
 | 
			
		||||
   For an introduction to socket programming (in C), see the following papers:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue