0

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
  • 2
    "Many Small Sockets vs One Big Socket" - what is that supposed to mean? A socket is a socket is a socket. It has no "size". Commented Sep 25, 2024 at 13:46
  • 1
    The buffer size of a socket does not say that a socket can transfer at most that many bytes. I guess there is a huge misconception here about how sockets work or what they are for that matter. Commented Sep 25, 2024 at 13:52
  • 1
    Are you sure you want to build this yourself? Have you checked existing solution, like MQTT, AMPQ, gRPC, or just http and concluded that none of them fulfill your requirements? Commented Sep 25, 2024 at 14:24
  • 1
    I agree, building a custom TCP protocol is so full of pitfalls, see blog.stephencleary.com/2009/04/tcpip-net-sockets-faq.html you really should not be doing this unless you have no other choice. Given you seem to lack even a basic understanding of how TCP sockets work, our concern seems to be warranted. Commented Sep 25, 2024 at 14:58
  • A socket represents the TCP connection itself. You don't send a socket. You send a data packet through a socket. Setting a buffer size on a socket has no bearing on the number of packets you can send. You can send as many packets as you want. So, to send a large file, you simply break it up into smaller chucks and send each chunk one after the other. It is also possible to send multiple files in parallel over a single socket connection, by interweaving the packets, and providing sufficient details on each packet to identify which file it belongs to. Commented Sep 25, 2024 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.