Java DatagramChannel class

Introduction

The following snippet of code shows three different ways to create a datagram channel:

Create a new datagram channel to send/receive datagram

DatagramChannel channel = DatagramChannel.open(); 

Create a datagram channel to receive datagrams from a multicast group that uses IPv4 address type

DatagramChannel ipv4MulticastChannel = DatagramChannel.open(StandardProtocolFamily.INET); 

Create a datagram channel to receive datagrams from a multicast group that uses IPv6 address type

DatagramChannel iPv6MulticastChannel = DatagramChannel.open(StandardProtocolFamily.INET6); 

To set option for DatagramChannel

channel.setOption(StandardSocketOptions.SO_REUSEADDR, true) 

Socket Option Name         Description                     Value

SO_SNDBUF                  The size of the socket send buffer in bytes.              Its value is of Integer type. 

SO_RCVBUF                  The size of the socket receive buffer in bytes.                   Its value is of Integer type. 

SO_REUSEADDR               For datagram sockets, it allows multiple programs to bind to the same address. This option should be enabled for IP multicasting using the datagram channels.               Its value is of  Boolean type. 
                           /*w ww.  j a v a 2 s . com*/
SO_BROADCAST               Allows transmission of broadcast datagrams.                                             Its value is of type  Boolean. 

IP_TOS                     The Type of Service (ToS) octet in the Internet Protocol (IP) header.                        Its value is of the Integer type. 

IP_MULTICAST_IF            The network interface for Internet Protocol (IP) multicast datagrams.                             Its value is a reference of NetworkInterface type. 

IP_MULTICAST_TTL           The time-to-live for Internet Protocol (IP) multicast datagrams.                                     Its value is of type Integer in the range of 0 to 255. 

IP_MULTICAST_LOOP          Loopback for Internet Protocol (IP) multicast datagrams.                              Its value is of type Boolean. 
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;

public class Main extends Thread {

   protected DatagramSocket socket = null;
   protected ByteBuffer message = null;

   public void run() {
      try {/* w w  w.  j a  va 2  s .  c o  m*/
         // send the response to the client at "address" and "port"
         InetAddress address = InetAddress.getByName("191.1.1.168");
         int port = 5239;

         DatagramChannel server = DatagramChannel.open().bind(null);
         System.out.println("Sending datagram packet to group " + address + " on port " + port);
         ByteBuffer message = ByteBuffer.wrap("Hello to all listeners".getBytes());
         server.send(message, new InetSocketAddress(address, port));

         server.disconnect();
      } catch (IOException e) {
         e.printStackTrace();

      }
   }

   public static void main(String[] args) {
      Main server = new Main();
      server.start();
   }

}



PreviousNext

Related