Linux UDP Programming Tutorial

UDP has its advantages over TCP, such as being relatively lightweight and receiving one packet per read call (recvmsg), although the programmers need to handle related issues, such as packet lost and out-of-order packets delivery. This post gives information and references on how to write UDP programs in a C/Linux environment.

What is UDP

Check the wikipedia article on UDP User Datagram Protocol for a brief overview. RFC 768 defines the standard of the UDP. For an introduction to TCP and UDP, the wiki book TCP and UDP Protocols is a good source.

UDP Programming in C on Linux/Unix

For beginners to UDP programming, I recommend the easy to follow tutorial UDP makde simple. This tutorial can help you quickly get started writing UDP programs.

For more details on UDP programming including UDP sockets and detailed explanation of the data structures used, check this tutorial: Programming with UDP sockets. This worksheet Introduction to User Datagram Protocol on *nix with C can also be a good learning material.

If you would like to study even more details, check Beej’s Guide to Network Programming.

As aforementioned, UDP has its complexities for programming with. For reliable bulk data transfer, UDT is a possible good option.

UDT: UDP-based Data Transfer

UDT is an efficient and easy to use library to transfer bulk data with its own reliability control and congestion control mechanisms. Being fast is one of UDT’s key features. Besides, concurrent UDT flows can share the available bandwidth fairly and UDT is also friendly to TCP flows.

For the design of UDT, check the paper UDT: UDP-based data transfer for high-speed wide area networks and Supporting Configurable Congestion Control in Data Transport Services. For more technical details, the Internet-Draft UDT: UDP-based Data Transfer Protocol is a good point to start with. And of course, you can read the source code of UDT which is opened under BSD license (since UDT version 4).

Programming Using UDT

The UDT manuals provides easy to follow guide on installation and programming tutorial: UDT 4 documents.

To check out the UDT source code from its git repository on Sourceforge:

$ git clone git://git.code.sf.net/p/udt/git udt-git

As UDT is a C++ library, to use it in C or other languages, you need to wrap the APIs in C (for languages other that C, you may need to wrap the C functions again). Tom Zhou already provides a C wrapper implementation also under the BSD license which can be downloaded here.

Have other good suggestions on UDP programming? Share them in the comments.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

One comment:

  1. One important concept in UDP programming is that a UDP packet is sent as a whole. The order inside the UDP packet is ensured.

    Datagram size

    “A field that specifies the length in bytes of the entire datagram: header and data. The minimum length is 8 bytes since that’s the length of the header. The field size sets a theoretical limit of 65,535 bytes (8 byte header + 65,527 bytes of data) for a UDP datagram. The practical limit for the data length which is imposed by the underlying IPv4 protocol is 65,507 bytes (65,535 − 8 byte UDP header − 20 byte IP header).” — from wikipedia.

Leave a Reply

Your email address will not be published. Required fields are marked *