Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 18 in printed book and digital formats -- plus a one-year subscription (beginning with 19.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview

Buy Now

Issue 4.1


Networking 201

The Ins and Outs of UDP

Issue: 4.1 (September/October 2005)
Author: Aaron Ballman
Author Bio: Aaron is the REAL Software networking swami and spends his spare time working on more projects than he can count, playing video games and assembling an army of wombats so he can take over the world.
Article Description: No description available.
Article Length (in bytes): 15,341
Starting Page Number: 27
Article Number: 4112
Related Link(s): None

Excerpt of article text...

Last time, we talked about what TCP is and how to use the TCPSocket class in REALbasic to communicate with another application over the network. In this article, we're going to cover another well-known protocol that REALbasic provides access to: the UDP protocol.

The basics of UDP

UDP is the User Datagram Protocol, which is a fast, unreliable way to send data across the network. It is different than the TCP protocol in a number of ways. For starters, it's a "connectionless" protocol. This means that setting the socket up works differently for a UDPSocket than it does for a TCPSocket. Also, the data transmission is unreliable, so you can't rely on packets you've sent reaching their destination. Despite these limitations, UDP has some very nice qualities to it. For instance, UDP data transmission is faster than TCP transmissions are, and you can send the same data to multiple machines using one call to Write. As you can see, there are some upsides and some downsides to using UDP. Let's talk a bit about when you want to use UDP instead of TCP.

UDP can be useful in a number of different instances. If your application needs more speed than TCP can provide, needs to send identical data to multiple machines, or doesn't require the data to be reliable then you should probably be using UDP. These types of situations typically involve sending large amounts of small data. If you need to send larger data (over 1K at a time), or you require the data to be sent reliably, chances are you should be using TCP instead of UDP. If you try to modify the UDP protocol with your own home-grown protocol in order to make it more reliable, then you should consider using TCP instead because that's what it's already designed to do. A few examples of applications which could make good use of UDP are: networked video games, voice over IP (VoIP) and streaming videos. Notice that one common thread between all of these examples is that some data loss is an acceptable outcome so long as the speed gains over TCP are fairly high. So what exactly is UDP and how does it differ from TCP under the hood?

At the very basics, all networking traffic is the same. It's just a bunch of bits that flow along a wire. As you learned in the first article, how those bits are arranged is what defines a protocol. TCP has a large header that gets passed along with every chunk of data you send. This header defines things like the sequence number and other tidbits of information that allows the network stack to make the protocol reliable. UDP has a much smaller header to it. So right off the bat, sending one byte of data with UDP will end up sending less actual data (your data plus the header) than TCP will. But the real speed gains come from the fact that UDP's header is so small since it doesn't try to do anything fancy with the data you are sending. If a UDP packet is dropped, the networking stack doesn't make any attempt to get that packet back like TCP does. This means that a large amount of overhead that TCP has isn't present in UDP transfers. Also, since UDP is a connectionless protocol, data transfers are a little different than they are with TCP. For example, you can send data to anyone after binding to a port on the local machine. Similarly, anyone can send data to your computer without your being ready for it! Because of this, you have to be very careful since it means that you can (and probably will at some point) receive data that is totally unexpected. In addition, because UDP does not guarantee reliability, you can have packets dropped, duplicated or arrive out of order. So be sure that code very defensively; don't assume that all the data you get in make sense. That's enough theory for now. How about we talk about how to use the UDPSocket in REALbasic?

...End of Excerpt. Please purchase the magazine to read the full article.