Socket error: 10053 software caused connection abort

Irmen de Jong irmen at -nospam-remove-this-xs4all.nl
Thu Apr 15 14:54:06 EDT 2004


Jean-Pierre Bergamin wrote:
> 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.

I have the same error here, and it is NOT because of a bug in Windows' TCP
stack. (even though the problem does not occur on Linux).

The problem is in your code.

You are receiving chunks of data like this:

>   # 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

But this code is flawed: recv() CAN return less than block_size,
*even* if there is more data to be read on the socket.

So, you should not check if the length of the received chunk
is less than the length argument passed to recv().

Instead, you should count the total number of bytes you
received and check that with the expected total amount.

Also, string appending is slow. Better append to a list and when
the loop is complete, return ''.join(alist).

I fixed your client and server receive loops in the mentioned way,
and they work fine even on windows :-)

However there is one more thing, when your block_size (the argument
to recv) is "too large" there are certain OSes that show problems
(VMS, Windows XP). MemoryErrors and such. I have been using:

	chunk=sock.recv(min(60000,block_size))

quite successfully.

--Irmen de Jong




More information about the Python-list mailing list