Sockets...

Steve Holden sholden at holdenweb.com
Wed May 9 12:22:51 EDT 2001


"Vincent A. Primavera" <vincent_a_primavera at netzero.net> wrote in message
news:mailman.989423353.4397.python-list at python.org...
> Hello,
> I have been experimenting with a variant of the program listed below from
> the Python Library Reference...  I'm getting the error below very
frequently.
>  How can I prevent this from happening?  And when it does happen is there
a
> way that I can 'manually' free up the 'socket'?
>
> Thank you,
> Vincent A. Primavera
>
> Error:
>
> Traceback (innermost last):
>   File "./server.py", line 37, in ?
>     auth()
>   File "./server.py", line 17, in auth
>     sck.bind((host, port))
> socket.error: (98, 'Address already in use')
>
> Program:
>
> # Echo server program
> import socket
>
> HOST = ''                 # Symbolic name meaning the local host
> PORT = 50007              # Arbitrary non-privileged port
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((HOST, PORT))
> s.listen(1)
> conn, addr = s.accept()
> print 'Connected by', addr
> while 1:
>     data = conn.recv(1024)
>     if not data: break
>     conn.send(data)
> conn.close()
>
I'm not absolutely positive about this: it's a hypothesis.

You close the connection, but not the socket. Without an explicit close it
could be that the TCP port is left in some awaiting timeout state for a
while, depending on the transport layer, before its state data are removed.

An explicit s.close() is unlikely to hurt, I should ahve thought. Let me
know if it helps.

regards
 Steve





More information about the Python-list mailing list