A
ahlongxp
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(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 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(32,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(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
for i in range(12):
print "time %d" % i
try:
s.send('0123456789')
except socket.error, e:
print "socket error:", e
break
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(socket.SHUT_WR)#no more write
'''
try changing 1 to 0.
'''
if 1:
data=s.recv(1024)
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(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 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(32,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(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
for i in range(12):
print "time %d" % i
try:
s.send('0123456789')
except socket.error, e:
print "socket error:", e
break
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(socket.SHUT_WR)#no more write
'''
try changing 1 to 0.
'''
if 1:
data=s.recv(1024)
else:
rf = s.makefile("rb")
data = rf.read()
rf.close()
s.close()
print 'Received', repr(data)