Buffer size for SendNetworkData and ReceiveNetworkData

Just starting out? Need help? Post your questions and find answers here.
sc4pb
User
User
Posts: 32
Joined: Tue Mar 07, 2023 5:33 pm

Buffer size for SendNetworkData and ReceiveNetworkData

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6270
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Buffer size for SendNetworkData and ReceiveNetworkData

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply