3

This is my first foray into UDP socket programming. Without getting into the details of the program, the server basically sends n messages of a set size to the client. I have written and ran the code, and it looks like all my messages are getting through to the client in exactly the order that the server sends them. Does this make any sense? It is my understanding that using UDP, the packets sent are not always delivered and not always delivered in the same order. If so, then why is the client program receiving all of the server messages in the same order and never losing any? I have experimented with different message numbers (e.g. sending 200 or 500 messages) and different message sizes. I am not using threads or anything like that. Just set up a loop for the server to send the packets one by one and the client just waits in an infinite loop to receive them. Insight from the more experienced people out there would be very helpful!

1
  • Just because you can lose packets doesn't mean you do. Try sending a few million packets as fast as you can, and measure if you lose any. Reordering usually happens on routers though, especially if it does load balancing of the packets.
    – nos
    Commented Nov 19, 2011 at 0:36

4 Answers 4

1

UDP is not guaranteed to be delivered or in order. That doesn't mean that it won't be, especially in your case where I assume your client and server are on the same machine (or on the same local network).

1

This depends upon your OS buffer mainly and UDP is not guaranteed to send/recieve in order so I think your sequential approach to send the data is the main reason.

1

UDP makes no guarantees about reliable delivery.

That is not the same as making a guarantee that the delivery is unreliable.

Try your peers on different continents and see what the packet loss / reordering / duplication is like. (I'm curious.)

1

Well the thing with UDP is that it usually works really well and it has a real low latency compared to TCP. That is probably the reason why it is used in almost every VoIP application, for real time video etc (see also RTP, RFC 3550) in almost every online game etc.

However, there is a catch :-)

UDP does not guarantee anything. As a consequence, that means that the UDP client application needs to be prepared for a lot of awful things including

  • packets not received at all
  • packets delayed
  • packets out of order
  • packet bursts

For real time communication applications, RTP has been developed in order to cope with those issues (see RTP). In addition to that - and to gain more control about the communication line, RTCP has been developed also (RFC 3605).

However, nobody will stop you from rolling your own protocol that ensures things will behave right when using UDP as a transport layer.

The bottom line is that when using UDP you will have to put a lot more effort into the client intelligence in order to get things right than you would have to using a stateful transport protocol.

1
  • The reason it is used in VoIP is that timeliness is much more important in that domain (and others), than guaranteed sequential delivery of every packet as in TCP.
    – user207421
    Commented Nov 19, 2011 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.