socket: connection reset by server before client gets response

ahlongxp ahlongxp at gmail.com
Sun Jul 8 00:53:59 EDT 2007


> Post the code.
ok.
here is the code:

# 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.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
conn.settimeout(1)
toread = 99
retrytime = 0
reads = 0
while reads < toread and retrytime < 10:
    try:
        data = conn.recv(min(32,toread-reads))
        if not data: continue
        print data
        reads += len(data)
    except:
        retrytime += 1
        print "timeout %d" % retrytime
        continue
if reads == toread:
    conn.send("OK")
else:
    conn.send("NOT OK")
conn.close()

****************I'm the separate
line*********************************************

# Echo client program
import socket

HOST = 'localhost'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
for i in range(12):
    print "time %d" % i
    s.send('0123456789')
    #data = s.recv(1024)
    #print "data %d" %i, data
#s.shutdown(socket.SHUT_WR)#no more write
data=s.recv(1024)
s.close()
print 'Received', repr(data)

client is supposed to get the response, either "OK" or "NOT OK".
but the fact is, client gets "Connection reset by peer" (as shown
below) about one in fifth.
----------------------------------
Traceback (most recent call last):
  File "c.py", line 10, in <module>
    s.send('0123456789')
socket.error: (104, 'Connection reset by peer')
----------------------------------

anyway, server is doing well all the time.

any comments on the design or implementation will be greatly
appreciated.

--
ahlongxp

Software College,Northeastern University,China
ahlongxp at gmail.com
http://www.herofit.cn




More information about the Python-list mailing list