UDP Server Algorithm

The User Datagram Protocol (UDP) Server Algorithm is a widely-used, connectionless, and efficient networking protocol that enables fast communication between servers and clients through the transfer of data packets. Unlike its counterpart, Transmission Control Protocol (TCP), which focuses on reliability and the establishment of a connection between devices, UDP prioritizes speed and simplicity, making it ideal for applications that require the rapid transmission of small amounts of data, such as online gaming, video streaming, and voice over IP (VoIP). Since UDP does not guarantee packet delivery, order, or error-checking, it is a lightweight protocol, enabling faster communication without the overhead of connection establishment and management. The UDP server algorithm begins by creating a socket, binding it to a specific port and IP address, and preparing it to listen for incoming data packets from clients. When a client sends a data packet, the server receives it and processes the data as needed, often by passing it to an application or service that is designed to handle the specific type of data being transmitted. Since there is no need for the server to establish a connection with each client or maintain a record of the communication, the server can handle multiple clients simultaneously, processing data packets as they arrive. If a response is required, the server can send a data packet back to the client, which may or may not be received depending on the network conditions. The simplicity and efficiency of the UDP server algorithm make it an attractive choice for applications that require high-speed data transmission and can tolerate the occasional loss of data packets.
// Server side implementation of UDP client-server model 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 

#define PORT	 8080 
#define MAXLINE 1024 

// Driver code 
int main() { 
	int sockfd; 
	char buffer[MAXLINE]; 
	char *hello = "Hello from server"; 
	struct sockaddr_in servaddr, cliaddr; 
	
	// Creating socket file descriptor 
	if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { 
		perror("socket creation failed"); 
		exit(EXIT_FAILURE); 
	} 
	
	memset(&servaddr, 0, sizeof(servaddr)); 
	memset(&cliaddr, 0, sizeof(cliaddr)); 
	
	// Filling server information 
	servaddr.sin_family = AF_INET; // IPv4 
	servaddr.sin_addr.s_addr = INADDR_ANY; 
	servaddr.sin_port = htons(PORT); 
	
	// Bind the socket with the server address 
	if ( bind(sockfd, (const struct sockaddr *)&servaddr, 
			sizeof(servaddr)) < 0 ) 
	{ 
		perror("bind failed"); 
		exit(EXIT_FAILURE); 
	} 
	
	int len, n; 
	n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
				MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
				&len); 
	buffer[n] = '\0'; 
	printf("Client : %s\n", buffer); 
	sendto(sockfd, (const char *)hello, strlen(hello), 
		MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
			len); 
	printf("Hello message sent.\n"); 
	
	return 0; 
} 

LANGUAGE:

DARK MODE: