Java DatagramChannel create broad casting server

Description

Java DatagramChannel create broad casting server

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 {//from  w  ww .  j  av a2s.c  om
         NetworkInterface networkInterface = null;
         java.util.Enumeration<NetworkInterface> enumNI = NetworkInterface.getNetworkInterfaces();
         java.util.Enumeration<InetAddress> enumIA;
         NetworkInterface ni;
         InetAddress ia;
         OUT: 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 OUT;
               }
            }
         }

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

         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();
      }
   }
}



PreviousNext

Related