JustTrying
JustTrying

Reputation: 602

C - Proper size for a buffer to be send via TCP

I'm writing a C client-server application.

The two sides exchange char buffer in order to communicate.

What is the proper size for these buffers?
Does exist a limit of bytes readable (or writable) by a read() (or a write()) on a stream-oriented socket?

Upvotes: 1

Views: 826

Answers (2)

brano
brano

Reputation: 2872

It depends if you are aiming for high throughput or low latency. Big buffers for high throughput and small buffers for low latency. Note also that when sending a buffer with x Bytes the read and write functions do not guarantee to send all the x bytes. Make sure to check the return value to see how many bytes was send/received continue sending/receiving the rest (this is often done with a while loop until you send/receive the whole buffer-size x).

Upvotes: 2

Steve Jessop
Steve Jessop

Reputation: 279205

Provided you write the code correctly there is no limit as long the connection is maintained. That's what a stream connection means.

Just remember that write() and read() can both return before they have written/read all of the data you provided/asked for. In that case the return value tells you how much was written/read, and it's your responsibility to call the function again to write/read any more.

Upvotes: 2

Related Questions