How to correctly send/receive data over UDP? - Unix

This is a discussion on How to correctly send/receive data over UDP? - Unix ; Hi all, I am having hard time trying to understand how I should use send/recv (sendto/recvfrom) with UDP sockets. The man pages (and other sources I tried) seem to discuss these function with no (or little) regards to the protocol ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: How to correctly send/receive data over UDP?

  1. How to correctly send/receive data over UDP?

    Hi all,

    I am having hard time trying to understand how I should use send/recv
    (sendto/recvfrom) with UDP sockets. The man pages (and other sources
    I tried) seem to discuss these function with no (or little) regards to
    the protocol being used, such as if they are completely polymorphic.
    Is it so?

    For example, with TCP, I know that I have to call send() untill all my
    data is sent. On the other side I have to call recv() as many time as
    needed untill all the data is read. If it's done this way I am
    guarantied that what is send is received in the same order (although
    generally in different chunks).

    Is it the same with UDP? What happens if my data don't fit in one
    package? Do I have control over how data is split into packages or
    does the system handle it? On the receiving side, do I receive
    packages in random order?

    Any help would be greatly appreciated. Also is there a good source
    that discusses these issues?

    Thanks,
    Arkadiy

  2. Re: How to correctly send/receive data over UDP?

    Arkadiy wrote:

    > Is it the same with UDP? What happens if my data don't fit in one
    > package?


    You cannot send more than around 64KB in one UDP datagram.

    > Do I have control over how data is split into packages or
    > does the system handle it?


    You specify want you want to send in a specific datagram, up to about
    64KB in size. The system will then handle getting it over to the
    destination.

    > On the receiving side, do I receive
    > packages in random order?


    It is possible that you may receive the datagrams out of order, but
    generally this is unusual. You must also handle packet loss yourself.

    > Any help would be greatly appreciated. Also is there a good source
    > that discusses these issues?


    Try searching "tcp/ip" on amazon.com. One of the best intermediate to
    advanced texts is "TCP/IP Illustrated" by W. Richard Stevens.

    Chris

  3. Re: How to correctly send/receive data over UDP?

    On Dec 6, 12:33 pm, Chris Friesen wrote:

    > You specify want you want to send in a specific datagram, up to about
    > 64KB in size. The system will then handle getting it over to the
    > destination.


    So, do I have to issue just one send()/sendto(), and as long as the
    data fits in one datagram, it will always accept everything in one
    shot? Or should I loop until everything is sent, like in TCP?

    > > On the receiving side, do I receive

    >
    > > packages in random order?

    >
    > It is possible that you may receive the datagrams out of order, but
    > generally this is unusual. You must also handle packet loss yourself.


    Again, how do I know what's a packet? Is one call to recv()/
    recvfrom() returns exactly one packet? Would the packet loss mean
    that the call to recv()/recvfrom() will block forever(or time out)?

    > Try searching "tcp/ip" on amazon.com. One of the best intermediate to
    > advanced texts is "TCP/IP Illustrated" by W. Richard Stevens.


    Actually the question was about UDP

    Did you mean "Unix network programming" by Richard Stevens?

    Thanks,
    Arkadiy

  4. Re: How to correctly send/receive data over UDP?

    Arkadiy writes:
    > On Dec 6, 12:33 pm, Chris Friesen wrote:
    >> You specify want you want to send in a specific datagram, up to about
    >> 64KB in size. The system will then handle getting it over to the
    >> destination.

    >
    > So, do I have to issue just one send()/sendto(), and as long as the
    > data fits in one datagram, it will always accept everything in one
    > shot? Or should I loop until everything is sent, like in TCP?


    A datagram socket is by definition one which preserves message
    boundaries, meaning, either what you specify as argument to a single
    send call gets send as single message or the call will return an
    error.

    >> > On the receiving side, do I receive

    >>
    >> > packages in random order?

    >>
    >> It is possible that you may receive the datagrams out of order, but
    >> generally this is unusual. You must also handle packet loss yourself.

    >
    > Again, how do I know what's a packet? Is one call to recv()/
    > recvfrom() returns exactly one packet?


    The same is true for receiving: Each sucessful call to recv on a
    datagram socket returns exactly a single message, as it was sent by
    the sender.

    > Would the packet loss mean that the call to recv()/recvfrom() will
    > block forever(or time out)?


    It will block until a message arrives. UDP-sockets are connectionless
    and UDP is an unreliable protocol, so there are no implicit timeouts.

  5. Re: How to correctly send/receive data over UDP?

    On Dec 6, 1:30 pm, Rainer Weikusat wrote:

    Thanks, got it about message boundaries.

    > It will block until a message arrives. UDP-sockets are connectionless
    > and UDP is an unreliable protocol, so there are no implicit timeouts.


    Does it respect SO_RCVTIMEO and SO_SNDTIMEO? They are defined on the
    socket level, not on the TCP level...

    Thanks,
    Arkadiy

  6. Re: How to correctly send/receive data over UDP?

    Arkadiy wrote:

    > Does it respect SO_RCVTIMEO and SO_SNDTIMEO? They are defined on the
    > socket level, not on the TCP level...


    According to the specification, it is implementation-defined as to
    whether these options can be set. Thus you need to look at the specific
    documentation for your particular version, and it is not safe to assume
    that these are generally available.


    Chris

  7. Re: How to correctly send/receive data over UDP?

    Arkadiy wrote:
    > On Dec 6, 12:33 pm, Chris Friesen wrote:


    >>Try searching "tcp/ip" on amazon.com. One of the best intermediate to
    >>advanced texts is "TCP/IP Illustrated" by W. Richard Stevens.


    > Actually the question was about UDP


    UDP is considered part of the TCP/IP stack as it is built on top of the
    IP stack.

    > Did you mean "Unix network programming" by Richard Stevens?


    No, I meant "TCP/IP Illustrated". Obviously you didn't even bother to
    look it up.

    Please, read a book on TCP/IP networking.

    Chris

  8. Re: How to correctly send/receive data over UDP?

    In article <13lgjt0ajodvp0b@corp.supernews.com>,
    Chris Friesen wrote:

    > Arkadiy wrote:
    > > On Dec 6, 12:33 pm, Chris Friesen wrote:

    >
    > >>Try searching "tcp/ip" on amazon.com. One of the best intermediate to
    > >>advanced texts is "TCP/IP Illustrated" by W. Richard Stevens.

    >
    > > Actually the question was about UDP

    >
    > UDP is considered part of the TCP/IP stack as it is built on top of the
    > IP stack.
    >
    > > Did you mean "Unix network programming" by Richard Stevens?

    >
    > No, I meant "TCP/IP Illustrated". Obviously you didn't even bother to
    > look it up.
    >
    > Please, read a book on TCP/IP networking.


    But since his question is about programming, I agree with Arkadiy that
    "Unix Network Programming" is a better recommendation. TCP/IP
    Illustrated is mostly about the implementation of the protocols, while
    Unix Network Programming is about how to use them in applications.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  9. Re: How to correctly send/receive data over UDP?

    Barry Margolin wrote:

    > But since his question is about programming, I agree with Arkadiy that
    > "Unix Network Programming" is a better recommendation. TCP/IP
    > Illustrated is mostly about the implementation of the protocols, while
    > Unix Network Programming is about how to use them in applications.


    For someone with no networking experience, I don't think that diving in
    to "Unix Network Programming" is the best option. Sure it covers how to
    do socket programming, but it doesn't discuss (as much, anyway) why to
    do something or what to do.

    In this case Arkadiy did not understand the concept of a datagram, and
    used the term "package" rather than "packet". When starting at that
    level, I think it would make sense to first learn more about networking
    (rather than network programming).

    In this case I think it makes more sense to first cover the protocols
    themselves, and then once he's got a grasp of what's happening from a
    conceptual standpoint then he should study how to actually write a
    program to make it happen.

    I will admit that Stevens is maybe a bit heavy for an introduction to
    the protocols though, so maybe that wasn't the best suggestion.

    Chris

  10. Re: How to correctly send/receive data over UDP?

    In article <13lhlj7tkpebl36@corp.supernews.com>,
    Chris Friesen wrote:

    > In this case Arkadiy did not understand the concept of a datagram, and
    > used the term "package" rather than "packet".


    With the name "Arkadiy", I suspect he's not a native English speaker.
    I've seen the term "package" used in this context quite a bit from
    non-English speakers, so I suspect that in some other languages the term
    they use is their translation of "package". I would never consider this
    an indication of lack of sophistication.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  11. Re: How to correctly send/receive data over UDP?

    Chris Friesen wrote:
    >
    > I will admit that Stevens is maybe a bit heavy for an introduction to
    > the protocols though, so maybe that wasn't the best suggestion.


    You could also take a look at TCP/IP Unleashed by Sams Publishing.

    Mark.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/


+ Reply to Thread