UDP Client-Server Implementation in C

There are two major communication protocols: TCP and UDP. These protocols are used to transport data between the client and the server. In one of my previous posts, we have implemented: TCP Client-Server Implementation in C

In this tutorial, we are going to build a simple UDP client-server program in the C programming language. Both the client and server are going to exchange data with each other over the network.

For this program, we are going to use the Ubuntu operating system, which provides a very flexible environment for building UDP client-server program.

Before we begin with the implementation of our program, let us start by understanding the UDP protocol.

What is UDP?

UDP stand for User Datagram Protocol and it’s a transport layer protocol. Some of the features of the UDP are:

  • UDP is an unreliable communication protocol.
  • It does not establish a connection before sending data.
  • It is much faster than the TCP.
  • No error control or flow control is provided by UDP.

How UDP is different from TCP?

TCP stands for Transmission Control Protocol and it’s also a transport layer protocol. TCP is a reliable communication protocol, as it establishes a connection before the transmission of the data. It provides assured delivery, reliability, and much more functionality. All these functionalities come with some overhead cost which makes TCP much slower than UDP.

UDP is preferred for real-time applications because it transmits the data without any overhead cost. UDP doesn’t perform any error-checking, it drops the packets instead of working on them. All these features make UDP a good choice for high-performance network services.

Implementation of UDP Client Server Program

Now, we start with the implementation of the UDP client-server program in the C programming language. We begin with the server program first.

Server

The server program would follow the following steps:

  1. Create a UDP socket.
  2. Bind the socket with the proper IP (Internet Protocol) adress and the port number.
  3. Wait for the datagram packet from the client.
  4. Process the datagram and send the reply.
  5. Finish.

The complete code for the server program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){

  if (argc != 2) {
    printf("Usage: %s <port>\n", argv[0]);
    exit(0);
  }

  char *ip = "127.0.0.1";
  int port = atoi(argv[1]);

  int sockfd;
  struct sockaddr_in server_addr, client_addr;
  char buffer[1024];
  socklen_t addr_size;
  int n;

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) {
    perror("[-]socket error");
    exit(1);
  }

  memset(&server_addr, '\0', sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = inet_addr(ip);

  n = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
  if (n < 0){
    perror("[-]bind error");
    exit(1);
  }

  bzero(buffer, 1024);
  addr_size = sizeof(client_addr);
  recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, &addr_size);
  printf("[+]Data recv: %s\n", buffer);

  bzero(buffer, 1024);
  strcpy(buffer, "Welcome to the UDP Server.");
  sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
  printf("[+]Data send: %s\n", buffer);

  return 0;
}

Client

The client program would follow the following steps:

  1. Create a UDP socket.
  2. Send a message to the server.
  3. Wait for the reply from the server.
  4. Process the packet.
  5. Finish.

The complete code for the client program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv){

  if (argc != 2) {
    printf("Usage: %s <port>\n", argv[0]);
    exit(0);
  }

  char *ip = "127.0.0.1";
  int port = atoi(argv[1]);

  int sockfd;
  struct sockaddr_in addr;
  char buffer[1024];
  socklen_t addr_size;

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  memset(&addr, '\0', sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = inet_addr(ip);

  bzero(buffer, 1024);
  strcpy(buffer, "Hello World!");
  sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
  printf("[+]Data send: %s\n", buffer);

  bzero(buffer, 1024);
  addr_size = sizeof(addr);
  recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
  printf("[+]Data recv: %s\n", buffer);

  return 0;
}

Run the UDP Client-Server Program

Now, we will compile both the server.c and the client.c and run the executable files.

Type the following command in the Terminal to compile the files.

$ gcc server.c -o server
$ gcc client.c -o client

I hope without any error or warning, the executable files would be created.

First, type the following command in a Terminal to run the server program.

$./server 4455

After this, type the following command to run the client program.

$./client 4455
Terminal screen showing the output of both the client and the server program.
Terminal screen showing the output of both the client and the server program.

Summary

In this tutorial, we have learned about the UDP and how it is different from the TCP. We have also implemented the UDP Client-Server program in the C programming language.

If you have any questions or queries. Contact me:

Nikhil Tomar

I am an independent researcher in the field of Artificial Intelligence. I love to write about the technology I am working on.

You may also like...

Leave a Reply

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