In this post we’ll create an example client and server that communicate over UDP in C using Unix as the Operating System. UDP is a connectionless, unreliable datagram packet service. The term connectionless means there is no handshake between the client and server before information is exchanged. The term “fire and forget” applies here since the client sends and beforehand has no negotiation with the server. The term unreliable means there is no guarantee of delivery, ordering or duplicate protection. UDP is best for time-sensitive applications where it is preferable to discard packets rather than waiting for delayed ones.
The advantages to UDP are two fold. Firstly, the latency to send the packets are reduced since there is no connection time. Secondly, the overhead is reduced when compared to TCP since a packet only has 8 bytes, whereas a TCP header is 20 bytes. UDP is lightweight and lean! Unreliability, depending on the network and amount of traffic can be overrated. In nearly all applications careful consideration should be made to decide TCP over UDP.
UDP Program Structure
The diagram below shows the Unix API usage for the UDP Server and Client example. Essentially, sockets are created and we employ
recvfrom to send information back and forth between the UDP socket.
It should be noted that many combinations of
recvfrom can be exchanged. In this example the server will “echo” back what is sent from the client. The client, by checking what was sent back from the server does have a means to know increase reliability.
UDP Server Unix API Calls
The UDP server will need to open a
socket and assign a socket address with
bind. Once, the socket is open and bound, then the server can receive using
recvfrom. If the server has received information from UDP it will know the client’s source port, and can send information back with
sendto if the client decides to receive. Finally, the server will close the socket.
UDP Server Socket, Bind, Network Interfaces and IPv4 versus IPv6
The UDP server socket needs to be bound to a network interface in Unix. In the example we are able to use the
getaddrinfo to identify a host and service from hints. This will allow us to eliminate any IPv4 and IPv6 dependences. Clients will also have the ability to connect using IPv4 or IPv6 because of how we open the socket and bind. The program will try to create the socket by it’s address family, here unspecified for IPv4 or IPv6, it’s socket type
SOCK_DGRAM and protocol.
It’s left as a further exercise to find the interface this socket is bound to. Also, depending on the interface that the socket binds to further setup maybe needed for IPv4 and IPv6.
UDP Client Unix API Calls
The UDP client is more simple than the server. I doesn’t need to
bind. It will simply open a
socket, and send information using
sendto then can receive back information, if desired, using
recvfrom. Note, the client will connect to the server from
AF_INET or IPv4, but could bind using IPv6.
Running the Example
The example requires both a UDP client and server to run. When the client connects to the server the server will print out the connection information, the data received, as well as the data it will send back. The client will also print out the data that it sends as well as the data it receives.
Running the UDP Server
Once we run the server we don’t see any output other than knowing it is running and didn’t exit. An improvement would be to print the interface, address and port that was bound.
Running the UDP Client
When the client is run, it will connect to the server and send 3
char with values
[1, 2, 16]. It will also print in parenthesis
(3) the number of bytes that were sent. Typically, in networking data is in units of “octets” which is fitting for the
char data type in C since it’s 8-bits.
Output of UDP client showing it sent 3 octets and their values.
Output on UDP Server from Client
The server will print the connection information received from the client. In this example it came from
localhost with a source port of
33887. The client will have a nearly random source port, whereas, the server is bound to port
5000. The UDP server, like the client will print out the value of the octets and how many it sends and receives.
UDP Server C Example
Below is a UDP Server example in C. It will bind to port
5000 and will send back to the client what it receives. This does put a requirement on the client to listen after it has sent.
UDP Client C Example
Below is an example UDP Client in C. It will connect to a server at
localhost on port
5000 and send 3 octets (
char in C), as well as, print out what the server sends it back.
Downloading the example.
If you want to run the example download the cudp example.