Broadcasting to a Group of Recipients - Java Network

Java examples for Network:Datagram Channel

Introduction

Client.java

Demo Code

import java.io.IOException;
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;

public class Main {
  public static void main(String[] args) {
    try {/*  w  w  w.  java  2 s.  co  m*/
      NetworkInterface networkInterface = null;
      java.util.Enumeration<NetworkInterface> enumNI = NetworkInterface
          .getNetworkInterfaces();
      java.util.Enumeration<InetAddress> enumIA;
      NetworkInterface ni;
      InetAddress ia;
      ILOOP: while (enumNI.hasMoreElements()) {
        ni = enumNI.nextElement();
        enumIA = ni.getInetAddresses();
        while (enumIA.hasMoreElements()) {
          ia = enumIA.nextElement();
          if (ni.isUp() && ni.supportsMulticast() && !ni.isVirtual()
              && !ni.isLoopback() && !ia.isSiteLocalAddress()) {
            networkInterface = ni;
            break ILOOP;
          }
        }
      }

      // Address within range
      int port = 5239;
      InetAddress group = InetAddress.getByName("226.18.84.25");

      final DatagramChannel client = DatagramChannel
          .open(StandardProtocolFamily.INET);

      client.setOption(StandardSocketOptions.SO_REUSEADDR, true);
      client.bind(new InetSocketAddress(port));
      client.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

      System.out.println("Joining group: " + group + " with network interface "
          + networkInterface);
      MembershipKey key = client.join(group, networkInterface);
      client.open();
      final ByteBuffer buffer = ByteBuffer.allocateDirect(4096);
      buffer.clear();
      System.out.println("Waiting to receive message");
      // Configure client to be passive and non.blocking
      // client.configureBlocking(false);
      client.receive(buffer);
      System.out.println("Client Received Message:");
      buffer.flip();
      byte[] arr = new byte[buffer.remaining()];
      buffer.get(arr, 0, arr.length);

      System.out.println(new String(arr));
      System.out.println("Disconnecting...performing a single test pass only");
      client.disconnect();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

Result

Server.java

Demo Code

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

public class Main extends Thread {
  ByteBuffer message = null;//w w  w  . j ava  2s. co  m
  public static void main(String[] args) {
    Main server = new Main();
    server.start();
  }
  @Override
  public void run() {
    try {
      // send the response to the client at "address" and "port"
      InetAddress address = InetAddress.getByName("226.18.84.25");
      int port = 5239;

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

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

Result

Use datagram multicasting using the DatagramChannel class.


Related Tutorials