Skip to content
On this page

UDP Programming

Compared to TCP programming, UDP programming is much simpler because UDP does not establish connections; data packets are sent and received one at a time, eliminating the concept of a stream.

In Java, UDP programming still requires the use of Sockets, as the application must specify the network interface (IP) and port number. Note that while UDP and TCP both use port numbers ranging from 0 to 65535, they are independent. For instance, a TCP application can use port 1234 without affecting a UDP application using the same port.

Server Side

To listen for UDP packets on the server side, you use DatagramSocket. Here’s an example code snippet:

java
DatagramSocket ds = new DatagramSocket(6666); // Listen on port 6666
for (;;) { // Infinite loop
    byte[] buffer = new byte[1024];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
    ds.receive(packet); // Receive a UDP packet
    String s = new String(packet.getData(), packet.getOffset(), packet.getLength(), StandardCharsets.UTF_8);
    byte[] data = "ACK".getBytes(StandardCharsets.UTF_8);
    packet.setData(data);
    ds.send(packet);
}

The server starts listening with:

java
DatagramSocket ds = new DatagramSocket(6666);

If the port is free, it enters an infinite loop to handle incoming UDP packets. You prepare a byte array buffer for receiving data via DatagramPacket. After receiving a packet, you convert the data to a String and send an acknowledgment back using the same DatagramPacket.

Client Side

The client-side code is simpler. It sends a UDP packet to the server and receives the response:

java
DatagramSocket ds = new DatagramSocket();
ds.setSoTimeout(1000);
ds.connect(InetAddress.getByName("localhost"), 6666); // Connect to server
byte[] data = "Hello".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length);
ds.send(packet);
byte[] buffer = new byte[1024];
packet = new DatagramPacket(buffer, buffer.length);
ds.receive(packet);
String resp = new String(packet.getData(), packet.getOffset(), packet.getLength());
ds.disconnect();
ds.close();

Here, the client creates a DatagramSocket without specifying a port, letting the OS choose an available one. The setSoTimeout(1000) method sets a timeout for receiving packets, preventing indefinite waiting. The connect() method does not establish a connection but records the server's IP and port, allowing the client to send packets only to that address.

If the client wants to communicate with multiple servers, it can either create multiple DatagramSocket instances or specify the server's address directly in the DatagramPacket:

java
byte[] data1 = "Hello".getBytes();
var packet1 = new DatagramPacket(data1, data1.length, InetAddress.getByName("localhost"), 6666);
ds.send(packet1);

Exercise

Implement server and client communication using UDP.

Summary

When communicating using the UDP protocol, both the server and client do not need to establish a connection:

  • Server Side: Use DatagramSocket(port) to listen on a specified port.
  • Client Side: Use DatagramSocket.connect() to specify the remote address and port.
  • Data Exchange: Use receive() and send() methods for reading and writing data.
  • Direct Buffer Access: DatagramSocket does not use IO stream interfaces; data is directly written to a byte array buffer.
UDP Programming has loaded