linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lorenzo Beretta <lory.fulgi@infinito.it>
To: linux-c-programming@vger.kernel.org
Subject: Re: UDP message boundary
Date: Fri, 19 Oct 2012 16:15:20 +0200	[thread overview]
Message-ID: <k5rn9n$iep$1@ger.gmane.org> (raw)
In-Reply-To: <CAA6iF_5RAWCB47=woDjb39aTfRH6qzUVsn2YdyTydUbyreL7Yg@mail.gmail.com>

On 17/10/2012 05:24, Randi Botse wrote:
> Anyone can share? :)
>
> Thanks
>
>
> On Mon, Oct 15, 2012 at 11:50 AM, Randi Botse<nightdecoder@gmail.com>  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
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
Almost:
1. udp is not reliable, so transmission may just fail
2. there's a maximum lenght of 64k (16 bit size counter)
3. ip fragmentation: as packet size grows, iirc one fragment lost means 
you loose everything

You can start from the wikipedia page and explore from that, or read the 
source of any program doing serious stuff with udp(*)


(*) there's an ftp alternative called fsp which uses udp, but I can't 
comment on the source code because I've never read it


  reply	other threads:[~2012-10-19 14:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-15  4:50 UDP message boundary Randi Botse
2012-10-17  3:24 ` Randi Botse
2012-10-19 14:15   ` Lorenzo Beretta [this message]
2012-10-19 15:03     ` Randi Botse
2012-10-20 21:55 ` Hendrik Visage
2012-10-21  9:46   ` Randi Botse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='k5rn9n$iep$1@ger.gmane.org' \
    --to=lory.fulgi@infinito.it \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).