socket.makefile () may lose data when "connection reset by peer".
and socket.recv() will never lose the data.
change the "1" to "0" in the client code to see the difference.
confirmed on both windows and linux.
so I guess there is a problem with makefile().
# Echo server program
import socket
HOST = '' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while(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(3 2,toread-reads))
if not data: continue
print data
reads += len(data)
except:
retrytime += 1
print "timeout %d" % retrytime
continue
#conn.shutdown( socket.SHUT_RD) #no more read
if reads == toread:
conn.send("OK")
else:
conn.send("NOT OK")
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.connect((HOST , PORT))
for i in range(12):
print "time %d" % i
try:
s.send('0123456 789')
except socket.error, e:
print "socket error:", e
break
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(soc ket.SHUT_WR)#no more write
'''
try changing 1 to 0.
'''
if 1:
data=s.recv(102 4)
else:
rf = s.makefile("rb" )
data = rf.read()
rf.close()
s.close()
print 'Received', repr(data)
and socket.recv() will never lose the data.
change the "1" to "0" in the client code to see the difference.
confirmed on both windows and linux.
so I guess there is a problem with makefile().
# Echo server program
import socket
HOST = '' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while(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(3 2,toread-reads))
if not data: continue
print data
reads += len(data)
except:
retrytime += 1
print "timeout %d" % retrytime
continue
#conn.shutdown( socket.SHUT_RD) #no more read
if reads == toread:
conn.send("OK")
else:
conn.send("NOT OK")
conn.close()
# Echo client program
import socket
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.connect((HOST , PORT))
for i in range(12):
print "time %d" % i
try:
s.send('0123456 789')
except socket.error, e:
print "socket error:", e
break
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(soc ket.SHUT_WR)#no more write
'''
try changing 1 to 0.
'''
if 1:
data=s.recv(102 4)
else:
rf = s.makefile("rb" )
data = rf.read()
rf.close()
s.close()
print 'Received', repr(data)
Comment