-1

Everyone who sends more than a handful of bytes over sockets has to somehow make the receiver know how many bytes belong to one packet and make the receiver reassemble incoming data until the eventual data packet is complete.
I have to do this too and I wonder if there is some helpful code for Linux C to accomplish this.

Actually to me one of the greatest mysteries of sockets is the question why there is no native support to this. Perhaps there is and I simply missed it?

I don't need an extremely oversized framework that allows all kind of complex configuration (and requires hours of understanding). Just an aid that does the accumulation of data packets and invoke a callback function when one packet is completed.

To make it a bit more clear. What I am about to implement on my own would be something like

  • a function that gets data, data size and an open socket. It adds size to the data and sends data over the socket.
  • a function that I forward received data to, plus the socket data came from plus a callback function. The function assembles data depending on the socket. When the packet is complete, the callback is invoked so the callback gets exactly the data packet that was sent.

Having something like this already implemented would be very helpful and probably less error-prone than what I would do.

Droidum
  • 440
  • 2
  • 9
  • 1
    Depends a bit on what you want. TCP doesn't give framing, but UDP does (at the expense of losing reliability). There are libraries for reliable UDP, and Websockets (among others) gives framed TCP. – Jerry Coffin Aug 27 '23 at 17:23
  • Those who downvote are invited to give a reason. Do you know something like that? Then help me find it. – Droidum Aug 27 '23 at 17:40
  • You'll want a TLV encoding. https://en.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value – AKX Aug 27 '23 at 17:41
  • See some of my answers: [Best practice of sending data of different sizes over the network](https://stackoverflow.com/a/63105194/5382650) [Checksum field of a packet, should it be at head or tail?](https://stackoverflow.com/a/68867189/5382650) – Craig Estey Aug 27 '23 at 17:42
  • To comments above, my data already is structured inside of the large blob that is sent. All I need is to send this large blob and get it assembled back to what it should look like on the receiver side. I would get that done somehow on my own, but I simply wonder why obviously everyone does that on their own again and again because nothing seems to exist? – Droidum Aug 27 '23 at 17:51
  • Whatever you want send must use the basic IP protocol to comply with network transport. said that if none of the higher level protocols, i.e. TCP or UDP, fits your requirements you can have a look to raw sockets. They expose the lower level transport data structures upon which you can eventually create your own protocol. – Frankie_C Aug 27 '23 at 18:15
  • @Droidum Because the details depend entirely on the protocol in use. Some send a fixed size byte count followed by the byes, others use a specific byte or set of bytes as a message delimiter. – dbush Aug 27 '23 at 18:32
  • If you want to send a blob of data together with its length, send the length and then the blob. Why do you need a framework for that? And why are sockets more special in this respect than say, regular disk files? – n. m. could be an AI Aug 27 '23 at 18:36
  • @dbush "Because the details depend entirely on the protocol in use." Yes of course. But if I only want to get a block of bytes sent, I don't care much about the protocol. That's why I thought there could be some code available that does this just somehow. Bytes in on one side, the same out at the other side. Whatever is in the middle, I don't care as long as it works :-) – Droidum Aug 27 '23 at 19:10
  • @n.m.couldbeanAI "Why do you need a framework for that" Assembling all that requires some work and opens doors for errors. What I could need is not an entire and complex framework, just a set of functions that do this better than if I implement it on my own. Or just see how others do that. "And why are sockets more special in this respect than say, regular disk files?" a disk file tells me when all bytes of the file are read. A socket doesn't, instead it could give me data from the next request, or perhaps nothing although more data is available but delayed and will come when I wait some more. – Droidum Aug 27 '23 at 19:13
  • FYI 1) comments are not for extended discussion; 2) nobody needs to justify votes, whether up or down; and 3) this is off-topic, as there is no programming question- it’s a requirement and a request for others to provide code for you, without you showing your own work. See [ask] and how to create a [mcve] – David Makogon Aug 27 '23 at 19:28
  • A very simple protocol would be the sender sending a byte with the length of the next chunk (< 256 bytes), zero meaning end-of-packet. Sender keeps sending chunks until the whole (app-defined) packet is sent, and then a zero byte. – 500 - Internal Server Error Aug 27 '23 at 19:32
  • @DavidMakogon 1. It wouldn't make much sense to implement something, only to ask then if this already exists somewhere else, would it? 2. I never said I wanted someone to write code for me. 3. This and the style of your comment unfortunately makes your comment appear as not more than a rant ;-) – Droidum Aug 28 '23 at 05:16

0 Answers0