Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

tcp

sockets

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

like image 767
Nick Bolton Avatar asked Mar 20 '09 12:03

Nick Bolton


People also ask

How many bytes can TCP send?

For a TCP socket, the maximum length that you can specify is 1 GB. For a UDP or RAW socket, the maximum length that you can specify is the smaller of the following values: 65,527 bytes (for a UDP socket) or 32,767 bytes (for a RAW socket).

How can TCP connection be improved?

Slow-Start Restart. Disabling slow-start after idle will improve performance of long-lived TCP connections that transfer data in periodic bursts. Window Scaling (RFC 1323) Enabling window scaling increases the maximum receive window size and allows high-latency connections to achieve better throughput.

How much data can TCP send?

The maximum size of a TCP packet is 64K (65535 bytes). Generally, the packet size gets restricted by the Maximum Transmission Unit (MTU) of network resources. MTU is the maximum size of the data transfer limit set by hardware in a network. The packet size should never exceed MTU.

How does TCP determine packet size?

The internet's transmission control protocol (TCP) uses the MTU to determine the maximum size of each packet in any transmission. MTU is usually associated with the Ethernet protocol, where a 1500-byte packet is the largest allowed in it (and hence over most of the internet).


1 Answers

The client and server will break up your data as they see fit, so you can send as much as you like in one chunk. Check A User's Guide to TCP Windows article by Von Welch.

like image 188
Jon B Avatar answered Oct 10 '22 23:10

Jon B