Home  >  

Using UDP socket connections for low-latency and loss-tolerant scenarios in AIR 2 (Part 1)

Author photo
November 17, 2009 | | Comments (3)
AddThis Social Bookmark Button

Flash player has supported socket connections for some time but has lacked UDP specific support. Although there was the recent addition of RTMFP (a protocol utilizing UDP to enable low latency data transfer and P2P connections) it doesn't provide direct access to UDP itself.

Not to worry though, because Adobe AIR 2 comes complete with a UDP socket class!

flash.net.DatagramSocket

What we'll cover.

This will be a multi-part series covering the theory and application of UDP, an AIR 2 client side implementation, and a basic server side implementation of a UDP socket server in Java.

What is UDP?

"Universal Datagram Protocol".

But first lets look look at its counter-part, the "Transmission Control Protocol". When you send data over TCP it does a good deal of additional work to ensure that everything arrives to its destination safely. It also makes sure that things arrive in the right order. These two features require that more information about the data (otherwise known as "control data") be sent along in TCP packet headers in order to keep track of things. This means additional processing is required before the data is sent and also after the data is received. When it encounters network congestion TCP can also down-throttle bandwidth so as to not completely overwhelm the internet during periods of high volume. Whereas TCP is the best choice for a majority of communications there are times when it is not the best fit. The additional processing involved in TCP and its potentially throttled bandwidth can translate to higher CPU usage and higher latencies.

The UDP protocol lacks all of the transmission management that TCP provides. It doesn't require a 3-way handshake in order to begin sending or receiving data. It doesn't ensure that all data sent is received or that packets are in the right order. 
UDP blindly sends data with a blatant disregard for its integrity. This means its packets don't need to carry control data in their headers and remain lightweight. It also means there is no additional processing to hold up the data before it is sent or received. Without additional overhead and processing UDP can perform faster than TCP. There is, however, a trade off: UDP is prone to data loss. As you would expect this is bad in most cases. On the other hand this makes UDP ideal for situations that require low latency and where packet loss is inconsequential.

When is UDP needed?

Here are just a few cases where UDP succeeds:

2-way video and
2-way audio streaming
 (aka VOIP)
Online multiplayer games


Consider the two-way video streaming found in video conferencing. Lets compare this to one-way video streaming known as video broadcasting. Broadcasting attempts to minimize frame loss and prevent choppy playback through a buffering system. Data integrity is important in this case and a TCP connection is typically used so lost packets can be re-sent (In fact RTMP is a TCP based protocol). Consequently there can be a considerable delay between when data is sent and data is received. The end user doesn't need to see things the exact moment they happen so a reasonable delay here is acceptable. The user would rather watch video without interruptions even if things aren't truly real time.

Now imagine the difficulty in holding a video chat with a person who only hears and sees you a number of seconds after you've spoken. In order for the two-way communication to work a low latency is required so that data is received as close to real time as possible. Additionally any potential data loss doesn't really matter. Anything that was missed from moments ago can be discarded in favor of showing current data. In this case a loss tolerant nature and a reliance on low latency calls for UDP.

VOIP is similar in nature to video conferencing so the same applies to it as well.

Another scenario where UDP excels is online multi-player gaming. You've more than likely experienced the frustration of a dead-on shot that doesn't register properly when playing your favorite online game. This typically happens when a player's position data is changing faster than notifications can be received by others. In addition to implementing position prediction algorithms UDP can be used to minimize this effect by providing a lower latency update to connected users. Data loss can also be tolerated since new position data is always on its way.

Lastly its important to mention that since UDP doesn't do any additional processing of its data there is far less strain on the CPU during times of high volume on a system. This means that a UDP server can maintain a greater number of simultaneous connections without running out of system resources. In the case of a multi-player server or a video conference this could mean the difference between supporting 500 concurrent users and 1000 on a single machine.

Now that we've identified the scenarios where UDP is needed the next thing we'll cover is the creation of a basic UDP client using AIR 2.


See part 2 of this series here

See part 3 of this series here

Read more from Ian McLean. Ian McLean's Atom feed ianmclean on Twitter

Comments

3 Comments

chad calease said:

Seems I've heard more from the proponents of TCP regarding error correction, rather than the robustness of UDP and its merits in content delivery, as what you state herein:

"Anything that was missed from moments ago can be discarded in favor of showing current data. In this case a loss tolerant nature and a reliance on low latency calls for UDP."

Your article is spot on - thank you for it.

Cheers.

=
c

Chris Deely said:

Great explanation of the often misunderstood differences between UDP and TCP. Can't wait for the next installment.

Chris

Mustafa said:

I am writing a UDP client/server socket program in which i want to send real time video from server to play it in the client. I am using UDP socket for this.
The problem is that how i send real time stream to the client. i am using avi streaming server in c#. every where including in this article i see code regarding sending and receiving text but i dont find any help regarding sending video stream or video frames...

can any body help me ????....

Leave a comment


Type the characters you see in the picture above.


Tag Cloud

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.