UDP Client Algorithm

The User Datagram Protocol (UDP) Client Algorithm is a simple, connectionless networking protocol that allows for the efficient transmission of data between client and server applications without the need for establishing a connection. Unlike its counterpart, the Transmission Control Protocol (TCP), UDP does not offer guaranteed delivery, error-checking, or sequencing of messages, making it ideal for use in applications that prioritize speed and low overhead over reliability. Common use cases for UDP include real-time applications like video streaming, online gaming, and Voice over IP (VoIP) services, where data loss or reordering is tolerable but low latency is crucial. The UDP Client Algorithm begins with the client application creating a UDP socket, which serves as the endpoint for sending and receiving data. The client then constructs a message, typically in the form of a datagram or packet, and specifies the target server's IP address and port number. The client sends the message by transmitting it through the socket, and the server receives the message at its own socket. As there is no connection established between the client and server, the client can send multiple messages to different servers using the same socket. Since UDP does not provide congestion control or error checking, the client must be prepared to handle any lost or out-of-order messages, usually by implementing custom error handling mechanisms or simply ignoring the lost data. Additionally, due to its connectionless nature, UDP offers no inherent method for acknowledging receipt of messages, so the client may need to implement application-level acknowledgments if this functionality is required.
// Client 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 client"; 
	struct sockaddr_in	 servaddr; 

	// 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)); 
	
	// Filling server information 
	servaddr.sin_family = AF_INET; 
	servaddr.sin_port = htons(PORT); 
	servaddr.sin_addr.s_addr = INADDR_ANY; 
	
	int n, len; 
	
	sendto(sockfd, (const char *)hello, strlen(hello), 
		MSG_CONFIRM, (const struct sockaddr *) &servaddr, 
			sizeof(servaddr)); 
	printf("Hello message sent.\n"); 
		
	n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
				MSG_WAITALL, (struct sockaddr *) &servaddr, 
				&len); 
	buffer[n] = '\0'; 
	printf("Server : %s\n", buffer); 

	close(sockfd); 
	return 0; 
} 

LANGUAGE:

DARK MODE: