I’m building a full-stack application where the server is to be capable of maintaining connections with multiple clients at the same time. Communication will be done via TCP-IP. Since most of the data transferred will be very small, I’ve compromised to set the buffer to 4096 bytes for both ends.
However, entire files will eventually be transferred through this same connection. Since these files may potentially be multiple MB in size, I’ve been questioning myself: which is the best approach, to send one large packet or send many 4096B packets to the client?
In the case of using one big packet, how will the client deal with it given that the client’s buffer is 4096 bytes as well? Will it “receive” many packets until the original packet is entirely consumed by the buffer or will the overflown data be lost?
8
When your packet is sent over the network, it will still be split into smaller parts of approximately 1.5 kilobytes. Therefore, in terms of network transmission, there won’t be a situation where you send several megabytes all at once. Regarding the buffer size, it indicates the amount of bytes that can be stored in the network buffer until you retrieve them. For instance, if requests are coming in so frequently that you don’t have time to process them, a large buffer size can prevent data loss, as it allows more data to be stored while waiting to be read by your code.
So it all depends on the load and the number of requests per second. If there are moments when the server cannot process the requests in time and they accumulate, it’s better to set a larger buffer size, for example, 128 kilobytes, and adjust it based on the situation.
Reidan Abdi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.