J
Jean-Pierre Bergamin
Hello there
I'm getting an error on Windows XP when I send big data chunks over a socket
connection. The exception I get is: "socket.error: (10053, 'Software caused
connection abort')"
This happens in the socket.recv function.
What I found out is that such errors can occur if there's not enough buffer
for receiving data in the underlying hardware.
On Linux it works fine. I guess, that the socket-code on windows could be
buggy and tries to read from a buffer whithout checking the length.
The server.py script does not alway receive all the data that was sent by
the client.
I'd be glad if someone could test theses scripts to see if this error also
occurs on other coputers and systems.
Or is there anything wrong with the code?
Regards
James
======================================================
server.py
======================================================
import SocketServer
class MyHandler(SocketServer.StreamRequestHandler):
def get_all(self):
# Get data as long as there's something on the line
data = ''
block_size = 1024
while(1):
chunk = self.connection.recv(block_size)
if not chunk:
break
data += chunk
if (len(chunk) < block_size):
# We got all the data
break
return data
def handle(self):
data = self.get_all()
data_len = len(data)
if (data_len == 0):
return
print "Received: %d" % data_len
total_sent = 0
while(total_sent < data_len):
sent = self.connection.send(data)
if not sent:
break
data = data[sent:]
total_sent += sent
try:
s = SocketServer.TCPServer(('', 12345), MyHandler)
s.serve_forever()
except Error, msg:
print msg
======================================================
client.py
======================================================
import sys
import socket
from time import sleep
port = 12345
host = 'localhost'
loops = 100
data_len = 12000
send_data = 'x' * data_len
to_send = len(send_data)
for _ in range(loops):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", port))
total_sent = 0
while(total_sent < to_send):
sent = s.send(send_data)
if not sent:
break;
print "Sent: %d" % sent
total_sent += sent
print "Total-sent: %d" % total_sent
# Get data as long as there's something on the line
data = ''
block_size = 1024
while(1):
got = s.recv(block_size)
if not got:
print "Connection broken"
break
data += got
if (len(got) < block_size):
# We got all the data
break
data_len = len(data)
s.close()
print 'Received bytes: %d' % len(data)
I'm getting an error on Windows XP when I send big data chunks over a socket
connection. The exception I get is: "socket.error: (10053, 'Software caused
connection abort')"
This happens in the socket.recv function.
What I found out is that such errors can occur if there's not enough buffer
for receiving data in the underlying hardware.
On Linux it works fine. I guess, that the socket-code on windows could be
buggy and tries to read from a buffer whithout checking the length.
The server.py script does not alway receive all the data that was sent by
the client.
I'd be glad if someone could test theses scripts to see if this error also
occurs on other coputers and systems.
Or is there anything wrong with the code?
Regards
James
======================================================
server.py
======================================================
import SocketServer
class MyHandler(SocketServer.StreamRequestHandler):
def get_all(self):
# Get data as long as there's something on the line
data = ''
block_size = 1024
while(1):
chunk = self.connection.recv(block_size)
if not chunk:
break
data += chunk
if (len(chunk) < block_size):
# We got all the data
break
return data
def handle(self):
data = self.get_all()
data_len = len(data)
if (data_len == 0):
return
print "Received: %d" % data_len
total_sent = 0
while(total_sent < data_len):
sent = self.connection.send(data)
if not sent:
break
data = data[sent:]
total_sent += sent
try:
s = SocketServer.TCPServer(('', 12345), MyHandler)
s.serve_forever()
except Error, msg:
print msg
======================================================
client.py
======================================================
import sys
import socket
from time import sleep
port = 12345
host = 'localhost'
loops = 100
data_len = 12000
send_data = 'x' * data_len
to_send = len(send_data)
for _ in range(loops):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", port))
total_sent = 0
while(total_sent < to_send):
sent = s.send(send_data)
if not sent:
break;
print "Sent: %d" % sent
total_sent += sent
print "Total-sent: %d" % total_sent
# Get data as long as there's something on the line
data = ''
block_size = 1024
while(1):
got = s.recv(block_size)
if not got:
print "Connection broken"
break
data += got
if (len(got) < block_size):
# We got all the data
break
data_len = len(data)
s.close()
print 'Received bytes: %d' % len(data)