Re: UDP message boundary

Randi Botse <[email protected]> Wed, 17 Oct 2012 10:24:08 +0700
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <CAA6iF_5RAWCB47=woDjb39aTfRH6qzUVsn2YdyTydUbyreL7Yg@mail.gmail.com>
Anyone can share? :)

Thanks


On Mon, Oct 15, 2012 at 11:50 AM, Randi Botse <[email protected]> wrote:
> Hi All
>
> When using TCP socket, I loop send() or recv() until ALL the data has
> been transmitted (or error, disconnect, etc.), because TCP socket
> packet is transmitted in stream nature, maybe a byte, bytes or all
> bytes in one transfer.
>
> The UDP socket preserve message boundary which TCP socket doesn't.
> Does this means single call to sendto() will processed by single call
> recvfrom()?, and how about packet that exceeds UDP data MAX size?.
>
> So in code, do I need to loop sendto() or recvfrom() to transmit the data?.
>
> Example codes is:
>
> char packet[100];
> size_t nbytes = 0;
> int ret;
>
> while (nbytes < sizeof(packet)) {
>     ret = recvfrom(socket, packet + nbytes, addr, 0,  sizeof(packet) - nbytes);
>     if (ret <= 0) {
>         /* deal with recvfrom() error */
>     }
>     nbytes += ret
> }
>
>
> Thanks