Page 1 of 1

Buffer size for SendNetworkData and ReceiveNetworkData

Posted: Thu Jun 15, 2023 8:54 pm
by sc4pb
What is the theory behind choosing a buffer size to use for sending and receiving data (tcp connections.)

Is there some wisdom in choosing larger or smaller sizes? Right now both my client and server appear to work fine with either 2k or 16k buffers. I'm guessing if I call ReceiveNetworkData with 2k buffers I'll just have to call it more times. When it comes to SendNetworkData with 2k or 16k, obviously same thing true in code, but does it also affect how the data is actually transmitted? And if so what are best practices?

Thanks

Re: Buffer size for SendNetworkData and ReceiveNetworkData

Posted: Thu Jun 15, 2023 9:24 pm
by mk-soft
You must always check the length of the data sent. The packet size in the local network is 1500 bytes. This means 1460 bytes of useful data per packet for TCP.
So it is best to always transmit a header with the data length (A LONG for example) and then read the data up to the length from the receive buffer. If you send two times, the second data can already be in the receive buffer. If you want to send more than 64kB, add a small delay (10ms) to avoid a send buffer overflow. The receiver must always run faster than the transmitter!

In any case, read up on how the TCP/IP protocol works on Wiki.