socket bindings

Nathan Clegg nathan at islanddata.com
Tue Jun 15 14:44:47 EDT 1999


I have a server application that binds itself to a specified port and
waits for connections.  It handles all connections fine and quits cleanly
after closing all of the connected sockets as well as the listening
socket.  However, if I start the application again, it will die, saying
the address is already bound to.  If I wait 2-3 minutes, the port will
become available again.  I can "solve" this by implementing a loop that
continues until the socket binds successfully, but this seems a little
klunky.  Can anyone tell me why the socket might not be correctly
releasing itself?  Relevant code follows:

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connected = None
    
    while not connected:
        try:
            server.bind('', PORT)
            connected = 1
        except socket.error:
            time.sleep(.1)
    server.listen(128)  # let the OS determine the limit
    print 'Listening...'

# code making connections deleted

    server.close()



----------------------------------
Nathan Clegg
 nathan at islanddata.com






More information about the Python-list mailing list