UDP Socket Programming in Unix Using C Programming
🎯 Introduction
In this blog post, we will explore UDP (User Datagram Protocol) socket programming in Unix using the C programming language. UDP is a connectionless transport protocol, providing a fast and efficient way to transmit data between networked devices. Unlike TCP (Transmission Control Protocol), UDP does not establish a connection before data transmission, making it lightweight and suitable for applications where low latency is critical.
🎯 Concept
UDP socket programming involves two main components: the server and the client. The server waits for incoming data from clients, processes the received data, and sends back the response. The client, on the other hand, sends data to the server and waits for a response.
🎯 Detailed Explanation UDP Socket Client and Server
Let's break down the provided code and understand how the Echo Server using UDP works.
👉 Server Code
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
int main() {
int sfd, l;
char buf[1024] = "", buf1[1024] = "";
struct sockaddr_in ser;
// Create a UDP socket
sfd = socket(AF_INET, SOCK_DGRAM, 0);
// Initialize server address
bzero(&ser, sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_port = htons(1300);
inet_aton("localhost", &ser.sin_addr);
// Get the message from the user
printf("Enter the message:");
scanf("%s", buf);
// Send the message to the server
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&ser, sizeof(ser));
// Receive the response from the server
recvfrom(sfd, buf1, 1024, 0, NULL, NULL);
// Close the socket
close(sfd);
return 0;
}
👉 Client Code
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
int main() {
int sfd, l;
char buf[1024] = "";
struct sockaddr_in server, client;
// Create a UDP socket
sfd = socket(AF_INET, SOCK_DGRAM, 0);
// Initialize server address
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(1300);
inet_aton("localhost", &server.sin_addr);
// Bind the socket to the server address
printf("bind=%d\n", bind(sfd, (struct sockaddr*)&server, sizeof(server)));
l = sizeof(client);
// Start the server loop
for (;;) {
// Receive data from the client
recvfrom(sfd, buf, 1024, 0, (struct sockaddr*)&client, &l);
// Send the response back to the client
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&client, l);
// Print the received message from the client
printf("MESSAGE FROM CLIENT: %s\n", buf);
}
return 0;
}
👉 Sample Output
Server:
cc udpechoserver.c -o echo
./echo
bind=0
MESSAGE FROM CLIENT:hello
Client:
cc udpechoclient.c -o udpecho
./udpecho
Enter the message:
hello
🎯 Summary
In this blog post, we discussed UDP socket programming in Unix using C. We explored an Echo Server and its corresponding client, which allows bi-directional communication between the server and the client. The server waits for incoming data, processes it, and sends back the response to the client. The client sends data to the server and waits for the server's response.
🎯 Key Points
UDP (User Datagram Protocol) is a connectionless transport protocol.
UDP socket programming is ideal for applications that require low latency and do not need the reliability and error recovery provided by TCP.
The server creates a UDP socket and waits for incoming data using recvfrom and responds using sendto.
The client creates a UDP socket and sends data to the server using sendto and waits for the server's response using recvfrom.
UDP is suitable for scenarios like real-time gaming, streaming, and other applications where timely delivery of data is more critical than guaranteed delivery. However, developers need to handle packet loss and reordering if necessary.