Sockets left in TIME_WAIT state ???

Philip Payne pnpayne at swissonline.ch
Sun Mar 5 08:18:27 EST 2000


Hi,

I'm brand new to sockets, in Python or otherwise, and I'm wondering whether
anyone else has experience of sockets not being cleaned up correctly (left
in TIME_WAIT state, whatever that means).

Using Python 1.5.2 I get the same behaviour on both Linux (2.2.5 kernel) and
Windows NT (4.00.1381).

It's easy to demonstrate on just one machine using the example programs in
the HTML documentation for the socket module.

# Echo client program

from socket import *
HOST = 'localhost'        # The remote host
PORT = 50007              # The same port as used by the server
s = socket(AF_INET, SOCK_STREAM)
s.connect(HOST, PORT)
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', `data`


# Echo server program

from socket import *
HOST = ''                 # Symbolic name meaning the local host
PORT = 50007              # Arbitrary non-privileged server
s = socket(AF_INET, 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()


After running the above server program and then client program in different
windows I get the following output from 'netstat'.

dellxps300:pnpayne:O815> netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:4019          localhost:50007
TIME_WAIT
        ....

Then after about 1 minute the networking software seems to clean up the
socket automatically by itself and 'netstat' doesn't show it any more.

Is this normal socket behaviour, a bug in the networking software or a bug
in the socket module that I should report???

Regards,
Philip Payne





More information about the Python-list mailing list