Java NIO DatagramChannel Tutorial

Java NIO DatagramChannel Tutorial

The DatagramChannel was introduced in Java 1.4 to allow developers to build high-performant data streaming applications that send and receive datagrams using a protocol called UDP. UDP (User Datagram Protocol) is one of the key protocols used in the internet. UDP uses a simple protocol which is connectionless and makes no quarantee of message delivery. UDP is sometimes referred to as Unreliable Datagram Protocol.

Just like in our previous tutorial, SocketChannel, DatagramChannels may be used with NIO Selectors.

Topics Covered in this Tutorial

  1. Opening a DatagramChannel
  2. Binding a DatagramChannel
  3. Connecting (Send/Receive to and from Specific Address)
  4. Checking if Connected
  5. Disconnecting (Send/Receive to and from any Address)
  6. DatagramChannel Server Example
  7. DatagramChannel Client Example

Opening a DatagramChannel

Since the java.nio.channels.DatagramChannel does not have a public constructor we must use the static open() method.

  public static DatagramChannel open( ) throws IOException

Example of Usage:

  DatagramChannel datagramChannel = DatagramChannel.open();

Binding a DatagramSocket

By using the bind(SocketAddress addr) method we configure the DatagramSocket to listen for inbound UDP packets.

In our example we use the static getLocalHost() method to retrieve the name of the host from the system, it then resolves that name into an InetAddress. We will use InetAddress object together with our port of 3333 and create an instance of InetSocketAddress. We then create an instance of DatagramChannel using the static open() method. At this time, the instance of DatagramSocket is not yet bound to any port.

  InetAddress hostIP = InetAddress.getLocalHost();
  InetSocketAddress address = new InetSocketAddress(hostIP, 3333);
  DatagramChannel datagramChannel = DatagramChannel.open();
  DatagramSocket datagramSocket = datagramChannel.socket();
  datagramSocket.bind(address);

Connecting (Send/Receive to and from Specific Address)

The DatagramChannel can be “connected”, or more precisely, it can be set up to send and receive datagrams to and from one specific host. However, since UDP is a connectionless protocol, the connect method returns immediately and does not create a real connection. Once connected, we are effectively only sending and receiving datagrams to and from one specific host. Datagrams from other hosts will be ignored or filtered.

public void connect(InetAddress address, int port)
                throws SocketException,  IllegalArgumentException

public void connect(SocketAddress addr) throws SocketException,
                                                IllegalArgumentException

Example of Usage:

  InetSocketAddress address = new InetSocketAddress("avaldes.com", 3333);
  DatagramChannel datagramChannel = DatagramChannel.open();
  datagramChannel.connect(address);

Checking if Connected

This method returns true if you has previously used the connect() method to connect, to limit transmission of datagrams to and from one specific host.

public abstract boolean isConnected()

Disconnecting (Send/Receive to and from any Address)

Just like the connect() method that creates a special “filter” to ensure that only datagrams from one specific host are received and we can only send datagrams to one specific host. The disconnect() method is used to remove the special filter created by connect(). At this point, our application will is able to send to and receive datagrams from any address.

public void disconnect()

Example of Usage:

  if (datagramChannel.isConnected()) {
    datagramChannel.disconnect();
  }

DatagramChannel Server Example

package com.avaldes.tutorial;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class MyDatagramServerExample {
 private static final int BUFFER_SIZE = 1024;
 static int port = 11;

 public static void main(String[] args) throws IOException {
  logger("Starting MyDatagramServerExample...");
  InetAddress hostIP = InetAddress.getLocalHost();
  InetSocketAddress address = new InetSocketAddress(hostIP, port);
  DatagramChannel datagramChannel = DatagramChannel.open();
  DatagramSocket datagramSocket = datagramChannel.socket();
  datagramSocket.bind(address);

  // Allocate a byte buffer
  ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);

  while (true) {
   datagramChannel.receive(buffer);
   buffer.flip();
   System.out.print("\nData...: ");
   while (buffer.hasRemaining()) {
    System.out.write(buffer.get());
   }
   buffer.clear();
  }
 }

 public static void logger(String msg) {
  System.out.println(msg);
 }
}

DatagramChannel Client Example

In this example, we use FileOutputStream and the getChannel() method to obtain a FileChannel.

package com.avaldes.tutorial;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyDatagramClientExample {
 private static final int BUFFER_SIZE = 1024;
 static int port = 11;
 private static List<String> myList = new ArrayList<String>(
   Arrays.asList("Tyrion Lannister",
          "Cersei Lannister",
          "Daenerys Targaryen",
          "Jon Snow",
          "Sansa Stark",
          "Arya Stark",
          "Jaime Lannister",
          "Jorah Mormont",
          "Lord Varys"));

 public static void main(String[] args) throws IOException {
  logger("Starting MyDatagramClientExample...");

    InetAddress hostIP = InetAddress.getLocalHost();
    InetSocketAddress myAddress =
        new InetSocketAddress(hostIP, port);
    DatagramChannel datagramChannel = DatagramChannel.open();
    datagramChannel.bind(null);

  ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
  for (String cast: myList) {
   logger("sending..: " + cast);
   buffer.put(cast.getBytes());
   buffer.flip();
   datagramChannel.send(buffer, myAddress);
   buffer.clear();
  }
 }

 public static void logger(String msg) {
  System.out.println(msg);
 }
}

That’s It!

I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

java_nio_datagramchannel

Java NIO Related Tutorials

  • Java NIO Tutorial
    In this tutorial series we discuss the new features of Java NIO.
  • Java NIO Buffers
    This post covers NIO Buffers in more detail and provides practical examples of using buffers in real world applications.
  • Java NIO Channels
    This post covers NIO Channels in more detail and provides examples on network connections and Java I/O in relation to files.
  • Java NIO Selectors
    In this tutorial we learn how to use the Selector class from the java.io.channels package library to build High-Performance I/O client-server using NIO.
  • Java NIO File Channel
    In this tutorial we learn how to use the FileChannel class from the java.io.channels package library and provide working examples on all of the main methods.
  • Java NIO Socket Channel
    In this tutorial we learn how to use the SocketChannel and how it is used for reading/writing stream oriented data and using TCP connection based protocol.
  • Java NIO DatagramChannel Tutorial
    In this tutorial we learn how to use the DatagramChannel to allow developers to build high-performant data streaming applications that send and receive datagrams using a protocol called UDP.
  • Java NIO and NIO2 Path Tutorial
    This tutorial will introduce the Path interface and many of its methods. The Path interface was made available as part of the Java SE 7 release in the Java NIO 2 File API.

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *