Sending files using Winsock - optimal send() data length?

Posted by Meta on Stack Overflow See other posts from Stack Overflow or by Meta
Published on 2010-04-18T22:48:44Z Indexed on 2010/04/18 22:53 UTC
Read the original article Hit count: 269

Filed under:
|
|

I am using Winsock with non-blocking sockets to send a file to a client. The way I'm doing it right now is that I read a chunk of 8192 bytes from the file, and then loop until all of it successfully goes through send() (obviously handling WSAEWOULDBLOCK as it occurs). I then move on and read the next 8192 bytes, and so on...

Although I can use any other number than 8192 when I test the transfer on my local machine, once I try it over a network, it seems like 8191 is the largest number I can use. When I try to use any number higher than 8191 (starting with 8192), the file transfer becomes extremely slow (about 5 times slower). Is there any reason why 8191 is so special?

I've done some more testing and it turns out that using 8000 is slightly faster (by 0.5%). If you understand why 8191 is so special, can you tell me if there is a number better than the others (better than 8000)?

I have a feeling that it has something to do with the fact that the default send buffer allocated to the socket by Winsock is 8KB, but I don't understand why. It might also have something to do with the Nagle algorithm, but again, I'm not sure how. Note that I have not modified the SO_SNDBUF option nor the TCP_NODELAY option.

Or am I doing this all wrong? What's the best way of sending a file over a non-blocking socket?

© Stack Overflow or respective owner

Related posts about winsock

Related posts about Windows