java.nio.channels.DatagramChannel class represents a datagram channel. By default, it is blocking. To make it non-blocking, use the configureBlocking(false) method.
To create a DatagramChannel
, invoke one of its open()
static methods.
To use it for IP multicasting, to specify the address
type of the multicast group as an argument to its open()
method.
The open()
method creates a DatagramChannel object, which is not connected.
The following code shows how to create an Echo Server Based on the Datagram Channel.
import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; /* ww w . java2 s . com*/ public class Main { public static void main(String[] args) throws Exception { DatagramChannel server = null; server = DatagramChannel.open(); InetSocketAddress sAddr = new InetSocketAddress("localhost", 8989); server.bind(sAddr); ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { System.out.println("Waiting for a message from" + " a remote host at " + sAddr); SocketAddress remoteAddr = server.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String msg = new String(bytes); System.out.println("Client at " + remoteAddr + " says: " + msg); buffer.rewind(); server.send(buffer, remoteAddr); buffer.clear(); } //server.close(); } }
The code above generates the following result.
The following code creates a Client Program Based on the Datagram Channel.
import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; //from w w w . j av a2 s . com public class Main { public static void main(String[] args) throws Exception { DatagramChannel client = null; client = DatagramChannel.open(); client.bind(null); String msg = "Hello"; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8989); client.send(buffer, serverAddress); buffer.clear(); client.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String response = new String(bytes); System.out.println("Server responded: " + response); client.close(); } }
The code above generates the following result.
import java.net.NetworkInterface; import java.util.Enumeration; /*from w w w . ja va 2s . co m*/ public class Main { public static void main(String[] args) throws Exception { Enumeration<NetworkInterface> e = NetworkInterface .getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface nif = e.nextElement(); System.out.println("Name: " + nif.getName() + ", Supports Multicast: " + nif.supportsMulticast() + ", isUp(): " + nif.isUp()); } } }
The code above generates the following result.
The following code A DatagramChannel-Based Multicast Client Program
import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.StandardProtocolFamily; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.nio.channels.MembershipKey; /*w w w. j a v a 2 s.co m*/ public class Main { public static final String MULTICAST_IP = "239.1.1.1"; public static final int MULTICAST_PORT = 8989; public static final String MULTICAST_INTERFACE_NAME = "eth1"; public static void main(String[] args) throws Exception { MembershipKey key = null; DatagramChannel client = DatagramChannel.open(StandardProtocolFamily.INET); NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME); client.setOption(StandardSocketOptions.SO_REUSEADDR, true); client.bind(new InetSocketAddress(MULTICAST_PORT)); client.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); InetAddress group = InetAddress.getByName(MULTICAST_IP); key = client.join(group, interf); System.out.println("Joined the multicast group:" + key); System.out.println("Waiting for a message from the" + " multicast group...."); ByteBuffer buffer = ByteBuffer.allocate(1048); client.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String msg = new String(bytes); System.out.format("Multicast Message:%s%n", msg); key.drop(); } }
The code above generates the following result.
The following code shows how to create a DatagramChannel-Based Multicast Program That Sends a Message to a Multicast Group.
import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; /*from w w w. java 2 s . c om*/ public class Main { public static final String MULTICAST_IP = "239.1.1.1"; public static final int MULTICAST_PORT = 8989; public static final String MULTICAST_INTERFACE_NAME = "eth1"; public static void main(String[] args) throws Exception { DatagramChannel server = DatagramChannel.open(); server.bind(null); NetworkInterface interf = NetworkInterface .getByName(MULTICAST_INTERFACE_NAME); server.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); String msg = "Hello!"; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); InetSocketAddress group = new InetSocketAddress(MULTICAST_IP, MULTICAST_PORT); server.send(buffer, group); System.out.println("Sent the multicast message: " + msg); } }
The code above generates the following result.