[Tutor] SocketObject.close() question

Andreas Schöller schoeller@zkm.de
Thu, 14 Feb 2002 11:49:32 +0100

> You can change a socket to reuse the same address, since the 
> socketserver
> class is being used, you can maybe get the xlmrpcsvr to take a =
> of that.
> class ReuseSocketServer(SocketServer):
>   def server_bind(self):
>     import socket
>     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>     SocketServer.server_bind(self)

Thank you for this example. I had two problems with it  ;-((  First i 
tried to subclass SocketServer which "is not a base class" then I tried  
to subclass BaseServer which is the baseclass for TCPServer which also 
did not work. As I don´t want to change the source-modules , i quit this 
approach. But while looking in the source I found a class-attribute 
named  'allow_reuse_address' which defaults to 0 ( python 2.1 & 2.2 
opposed to the docs, who is to be told to change this ? ).   After I 
recovered from my first euphoria i changed my script :

port = 9000
server = SocketServer.TCPServer(('', port), RequestHandler)
server.allow_reuse_address = 1

which also, did not work, because the attribute-setting comes too late. 
I ended up with changing the default in the module, with one question 
remaining :  How can one use this setting without subclassing ?

> Also, you might want to think about using the shutdown() method =
> of just close().  This will allow the network protocols to come down
> gracefully, and will reduce the number of "already in use" errors (not
> not all of them).  There is nothing strictly wrong with using close, =
> you will want to make sure that the socket isn't connected by some 
> means.
shutdown(2) is what i wanted, thanks..

> Think about a telephone: it is possible, though unprobable, that =
> could call at the same time you pick up the handset to make a call.
> This causes the phone to not ring, and the connection to go through;
> you start dialing the number and the other end is going "hello".
> This happens in real, but it's rare.  Now think about if it was at the
> other end of the call - someone was calling when you were hanging up,
> the connection might be established, your phone would think the call =
> done, but possibly the hardware in between left the connection open.
> The phone systems do not allow this "feature", but the TCP networks =
> It's better to just use "shutdown" to let the whole system know you =
> shutting down.