Alex Rivera | Logout

Is it better to send 1 large chunk or lots of small ones when using TCP?

Asked 2009-03-20T12:55:03.250
16

After I accept() a connection, and then write() to the client socket, is it better to write all the data you intend to send at once or send it in chunks?

For example:

accept, write 1MB, disconnect

…or…

accept, write 256 bytes, write 256 bytes, … n, disconnect

My gut feeling tells me that the underlying protocol does this automatically, with error correction, etc. Is this correct, or should I be chunking my data?

Before you ask, no I'm not sure where I got the idea to chunk the data – I think it's an instinct I've picked up from programming C# web services (to get around receive buffer limits, etc, I think). Bad habit?

Note: I'm using C

Edit
Report

1 Answer

3

If you're computing the data between those writes, it may be better to stream them as they're available. Also, writing them all at once may produce buffer overruns (though that's probably rare, it does happen), meaning that your app needs to pause and re-try the writes (not all of them, just from the point where you hit the overflow.)

I wouldn't usually go out of my way to chunk the writes, especially not as small as 256 byte chunks. (Since roughly 1500 bytes can fit in an Ethernet packet after TCP/IP overhead, I'd use chunks at least that large.)

answered 2009-03-20T13:02:06.337

Your Answer