User Datagram Protocol (UDP)
A Connectionless Transport Protocol
Shiva Acharya
UDP Protocol Introduction
• UDP = User Datagram Protocol (RFC 768)
• Developed by David P. Reed (1980)
• Enables process-to-process communication via datagrams
• Lightweight alternative to TCP (no handshaking/ACKs)
• Ideal for real-time applications (e.g., streaming, gaming)
Features of UDP
• Connectionless: No virtual circuit; packets sent independently
• Unreliable: No ACKs, retransmissions, or guaranteed delivery
• Low Overhead: Small header (8 bytes)
• Port-Based Routing: Uses port numbers (0–1023)
• Stateless: No session maintenance
Why Do We Require UDP?
• When Speed > Reliability:
• - Video streaming (loss-tolerant)
• - Online gaming (low latency)
• - DNS queries (small, fast requests)
• Bandwidth Efficiency:
• - No ACKs reduce overhead (e.g., SNMP, TFTP)
UDP Header Format
• Header Fields (8 bytes):
• 1. Source Port (16 bits): Sender's application port
• 2. Destination Port (16 bits): Receiver's port
• 3. Length (16 bits): Total packet size (header + data)
• 4. Checksum (16 bits, optional): Data integrity check
• Max Size: 65,507 bytes
Concept of Queuing in UDP(Process to process)
• Queues:
• - Input Queue: Receives incoming datagrams
• - Output Queue: Sends outgoing datagrams
• Components:
• - Control Block Table: Manages open ports
• - Input/Output Modules: Handle datagram routing
• Process Termination: Queues destroyed if process ends
UDP Applications
• DNS (Domain Name System): Resolves domain names to IPs
• SNMP (Simple Network Management Protocol): Monitors network devices
• TFTP (Trivial File Transfer Protocol): Lightweight file transfer
• VoIP (Voice over IP): Real-time voice communication
UDP Client Example
import java.net.*; // Import networking classes // Convert the message string into bytes to send
byte[] buffer = "Hello Server!".getBytes();
public class UDPClient {
public static void main(String[] args) throws Exception { // Create a UDP packet to send the message to the server's address and port
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address,
// Define the server's hostname (or IP address) port);
String hostname = "example.com"; // Replace with "localhost" or actual IP if
testing locally // Send the packet through the socket to the server
socket.send(packet);
// Define the server port number to which the client will send data
int port = 1234; // Optional: You can also add receive code here to get the server's response
// Resolve the hostname into an IP address // Close the socket (optional for short-lived clients)
InetAddress address = InetAddress.getByName(hostname); socket.close();
}
// Create a new DatagramSocket (no port needed for client) }
DatagramSocket socket = new DatagramSocket();
UDP Server Example
// Get the sender's IP address and port number
InetAddress clientAddress = packet.getAddress();
// Create a UDP socket and bind it to port 1234
int clientPort = packet.getPort();
DatagramSocket socket = new DatagramSocket(1234);
// Prepare the response message
// Create a byte array buffer to store incoming data
String responseMsg = "ACK";
byte[] buffer = new byte[256];
byte[] responseBuffer = responseMsg.getBytes(); // Convert response to byte
// Server runs continuously
// Create a new packet to send the response to the client
while (true) {
DatagramPacket response = new DatagramPacket(
// Create a DatagramPacket to receive data from clients
responseBuffer, // Data to send
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
responseBuffer.length, // Length of data
clientAddress, // Client's IP address
// Wait for an incoming datagram packet (blocking call)
clientPort // Client's port
socket.receive(packet);
);
// Convert the received byte data into a String
// Send the response packet back to the client
String received = new String(packet.getData(), 0, packet.getLength());
socket.send(response);
System.out.println("Received: " + received); // Print the received message
}
}
}
DatagramPacket Class
• Purpose: Container for data + addressing (IP + port)
• Creating a Packet:
• - For Receiving: DatagramPacket(byte[] buffer, int length)
• - For Sending: DatagramPacket(byte[] buffer, int length, InetAddress, port)
• Key Methods:
• - getData(): Retrieve packet content
• - getAddress()/getPort(): Sender's details
DatagramPacket Class Methods
Summary
• UDP = Fast, lightweight, connectionless
• Ideal for real-time apps despite unreliability
Q&A
• Thank You!