Author
UDP (User Datagram Protocol) is another important way of communication in Java Networking. It is a connectionless protocol and works faster than TCP but does not guarantee delivery of data. That is why it is used where speed is more important than reliability.
UDP sends data without establishing a connection and without confirming whether the data is received or not.
In this guide, you will learn what UDP is, how it differs from TCP, and how to build a simple UDP client-server program using DatagramSocket.
UDP (User Datagram Protocol) is a connectionless communication protocol used for fast data transfer over a network in Java Networking.
In simple words: UDP sends data packets directly without establishing a connection and without checking whether the data reached safely or in order.
Example: Live video streaming or online gaming where speed is more important than perfect delivery.

? Because of these features, UDP is used where speed is more important than accuracy and reliability, such as real-time applications.
UDP Server (Receiver)
import java.net.*;
public class UDPServer {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(
packet.getData(), 0, packet.getLength()
);
System.out.println("Client: " + message);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
UDP Client (Sender)
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket();
String msg = "Hello Server";
byte[] buffer = msg.getBytes();
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, address, 5000
);
socket.send(packet);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation
DatagramSocket → Used to start UDP communicationDatagramPacket → Used to create and carry data for sending/receivingsend() → Sends data to the receiver (server/client)receive() → Receives data from sender| Feature | TCP | UDP |
|---|---|---|
| Connection | Establishes connection before data transfer | Sends data without connection |
| Speed | Slower due to connection setup | Faster due to no setup |
| Reliability | Ensures delivery of data | No guarantee of delivery |
| Order of data | Data arrives in correct order | Data may arrive in any order |
| Overhead | High (extra checks & control) | Low (minimal processing) |
| Use case | Web browsing, banking, file transfer | Video streaming, online gaming, DNS |

DatagramSocket is a class in Java used to send and receive UDP packets.
? It works without a continuous connection between client and server.
In simple words: It sends data like “postcards” — no guarantee of delivery or order.
Example (Basic UDP Sender)
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello Server";
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(
data, data.length, address, 5000
);
socket.send(packet);
System.out.println("Message sent");
socket.close();
}
}
Example (Basic UDP Receiver)
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(
packet.getData(), 0, packet.getLength()
);
System.out.println("Client says: " + message);
socket.close();
}
}
DatagramPacket is used to store data that is sent or received in UDP communication.
? It acts like a container for data packets.
DatagramPacket provides that structureIn simple words: It is like a box that carries data from sender to receiver
A DatagramPacket stores:
? For receiving, it only stores:
Example (Sending Packet)
import java.net.*;
public class Sender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello UDP Server";
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(
data, data.length, address, 5000
);
socket.send(packet);
socket.close();
}
}
Example (Receiving Packet)
import java.net.*;
public class Receiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(
packet.getData(),
0,
packet.getLength()
);
System.out.println("Received: " + message);
socket.close();
}
}
UDP follows a simple flow for data transmission between sender and receiver.
Sender → Create Packet → Send via Socket
Receiver → Receive Packet → Read Data
? No connection setup is required before communication.
Explanation
DatagramPacket and then sent using DatagramSocketreceive() methodimport java.net.*;
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
System.out.println("Server is waiting for data...");
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received from client: " + message);
socket.close();
}
}
Explanation
DatagramSocket(5000) → Creates server socket on port 5000 and listens for incoming UDP packetsDatagramPacket → Stores incoming data from clientreceive() → Waits for data from client (blocking call)getData() → Extracts raw byte data from packetgetLength() → Returns actual length of received messageimport java.net.*;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello from UDP Client";
byte[] buffer = message.getBytes();
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(
buffer,
buffer.length,
address,
5000
);
socket.send(packet);
System.out.println("Message sent to server");
socket.close();
}
}
Explanation
DatagramSocket() → Creates client socket for sending UDP packetsgetBytes() → Converts string message into byte arrayInetAddress.getByName("localhost") → Defines server addressDatagramPacket → Packs data along with destination IP and port numbersend() → Sends UDP packet to serverStep-by-step flow:
DatagramSocketDatagramPacket with datasend()DatagramSocket on a portDatagramPacketreceive() and waits for packet
UDP is used in applications where speed and real-time communication are more important than guaranteed delivery:

UDP Communication in Java is a lightweight and fast way to transfer data between systems.
DatagramSocket for communication.DatagramPacket for sending and receiving data.? It is best suited for real-time applications where speed is more important than reliability.