C Programming: UDP Server/Client 🎯

beginner
19 min

C Programming: UDP Server/Client 🎯

Welcome to our comprehensive guide on creating a UDP (User Datagram Protocol) Server and Client in C programming! This tutorial is designed to be beginner-friendly, yet packed with in-depth knowledge that will be beneficial for both beginners and intermediates.

Understanding UDP 📝

UDP is a connectionless protocol used for sending datagrams (packets of data) over the internet. Unlike TCP, UDP does not guarantee the delivery, order, or duplication-free nature of the data. However, it is faster and more lightweight, making it suitable for real-time applications like video streaming, online gaming, and DNS queries.

Setting Up the Development Environment ✅

To write and run C programs, you'll need:

  • A text editor (e.g., Visual Studio Code, Sublime Text)
  • A C compiler (e.g., GCC, TCC)

On Linux and macOS, you can install the necessary tools with the following command:

bash
sudo apt-get install build-essential

On Windows, you can use MinGW-w64 or Cygwin to set up the environment.

Creating a UDP Server 💡

Let's create a simple UDP server that listens for incoming messages and sends a response.

c
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> #define PORT 8080 int main() { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addr_len = sizeof(address); char buffer[1024] = {0}; char reply[1024] = {0}; // Create the socket if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Set options on the socket if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt"); exit(EXIT_FAILURE); } // Configure the address address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // Bind the socket to the address if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // Keep listening for messages while (1) { // Receive the message ssize_t read_size = recvfrom(server_fd, buffer, 1024, 0, (struct sockaddr *)&address, &addr_len); if (read_size < 0) { perror("recvfrom"); exit(EXIT_FAILURE); } printf("Message received: %s\n", buffer); // Create a response message snprintf(reply, sizeof(reply), "Hello, %s! Your message: %s", inet_ntoa(address.sin_addr), buffer); // Send the response sendto(server_fd, reply, strlen(reply), 0, (struct sockaddr *)&address, addr_len); } return 0; }

Save this code in a file named udp_server.c and compile it using the command:

bash
gcc -o udp_server udp_server.c

Run the compiled binary with:

bash
./udp_server

Creating a UDP Client 💡

Now, let's create a simple UDP client that sends a message to our server and prints the response.

c
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> #define PORT 8080 int main() { int sock = 0, valread; struct sockaddr_in serv_addr; char buffer[1024] = {0}; char *hello = "Hello, Server!"; if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(EXIT_FAILURE); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Set the server's IP to 127.0.0.1 (localhost) for this example serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("connect"); exit(EXIT_FAILURE); } if (sendto(sock, hello, strlen(hello), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("sendto"); exit(EXIT_FAILURE); } valread = read(sock, buffer, 1024); buffer[valread] = '\0'; printf("Response from server: %s\n", buffer); return 0; }

Save this code in a file named udp_client.c and compile it using the command:

bash
gcc -o udp_client udp_client.c

Run the compiled binary with:

bash
./udp_client

Practical Applications 💡

The UDP Server and Client we've created can be modified to build real-world applications like a simple chat application, a real-time scoreboard for multiplayer games, or even a basic file transfer system.

Quiz 📝

Quick Quiz
Question 1 of 1

What is UDP used for in networking?