Search code examples
socketsunixbsddarwin

Read whole Message from UDP Socket


I'm sending data over an UDP socket and receive it in a loop with read(). The input data looks like this:

String 1
String 2
String 3
....

I write data out with send(), each string after each other (in a loop). How do I make sure that I can reconstruct the data on the receive end in the correct fashion (as I put the strings in)?

The received data can be split anywhere in the middle of the lines like so:

Packet 0: Stri
Packet 1: ng 1
Packet 2: String 2 St
Packet 3: ring 3
...

Do i have to introduce a custom END OF MESSAGE byte sequence to tell? Because EOF won't help here.

I need to be able to tell if a package is corrupted, and where the data blocks that belong together begin and end, as I sent them away beginning with S and ending with the Number! I can't use TCP because i need broadcast/multicast support.


Solution

  • If you want all messages to arrive, and in the same order they were sent, and to have an "end of message" indication, maybe TCP is better :-)

    (TCP does this all out of the box.)