Examples of MulticastSocket


Examples of java.net.MulticastSocket

        JoinConfig join = config.getNetworkConfig().getJoin();
        MulticastService mcService = null;
        try {
            if (join.getMulticastConfig().isEnabled()) {
                MulticastConfig multicastConfig = join.getMulticastConfig();
                MulticastSocket multicastSocket = new MulticastSocket(null);
                multicastSocket.setReuseAddress(true);
                // bind to receive interface
                multicastSocket.bind(new InetSocketAddress(multicastConfig.getMulticastPort()));
                multicastSocket.setTimeToLive(multicastConfig.getMulticastTimeToLive());
                try {
                    // set the send interface
                    final Address bindAddress = addressPicker.getBindAddress();
                    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4417033
                    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6402758
                    if (!bindAddress.getInetAddress().isLoopbackAddress()) {
                        multicastSocket.setInterface(bindAddress.getInetAddress());
                    }
                } catch (Exception e) {
                    logger.warning(e);
                }
                multicastSocket.setReceiveBufferSize(64 * 1024);
                multicastSocket.setSendBufferSize(64 * 1024);
                String multicastGroup = System.getProperty("hazelcast.multicast.group");
                if (multicastGroup == null) {
                    multicastGroup = multicastConfig.getMulticastGroup();
                }
                multicastConfig.setMulticastGroup(multicastGroup);
                multicastSocket.joinGroup(InetAddress.getByName(multicastGroup));
                multicastSocket.setSoTimeout(1000);
                mcService = new MulticastService(this, multicastSocket);
                mcService.addMulticastListener(new NodeMulticastListener(this));
            }
        } catch (Exception e) {
            logger.severe(e);
View Full Code Here

Examples of java.net.MulticastSocket

      {
         // use this to bind so multicast will work w/o network
         this.bindAddr = localHost;
      }
      SocketAddress saddr = new InetSocketAddress(bindAddr, port);
      socket = new MulticastSocket(saddr);
      socket.joinGroup(addr);

      super.start();

      if(listener == null)
View Full Code Here

Examples of java.net.MulticastSocket

         return;
      }

      try
      {
         socket = new MulticastSocket(groupPort);

         if (localBindAddress != null)
         {
            socket.setInterface(localBindAddress);
         }
View Full Code Here

Examples of java.net.MulticastSocket


   public void testMcastSocketCreation() throws Exception {
      InetAddress mcast_addr = InetAddress.getByName(IP_ADDRESS);
      SocketAddress saddr = new InetSocketAddress(mcast_addr, 43589);
      MulticastSocket retval = null;
      try {
         success = false;
         retval = new MulticastSocket(saddr);
         success = true;
      } finally {
         if (retval != null) {
            try {
               retval.close();
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
View Full Code Here

Examples of java.net.MulticastSocket

   }

   public void testMcastSocketCreation2() throws Exception {
      InetAddress mcast_addr = InetAddress.getByName(IP_ADDRESS);
      int port = 43589;
      MulticastSocket retval = null;
      try {
         Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
         StringBuilder okTrace = new StringBuilder();
         StringBuilder failureTrace = new StringBuilder();
         success = true;
         while (nis.hasMoreElements()) {
            retval = new MulticastSocket(port);
            NetworkInterface networkInterface = nis.nextElement();
            retval.setNetworkInterface(networkInterface);
            try {
               retval.joinGroup(mcast_addr);
               String msg = "Successfully bind to " + networkInterface;
               okTrace.append(msg).append('\n');
            } catch (IOException e) {
               e.printStackTrace();
               String msg = "Failed to bind to " + networkInterface + ".";
               failureTrace.append(msg).append('\n');
               success = false;
            }
         }
         if (success) {
            log.trace(okTrace);
            System.out.println("Sucessfull binding! " + okTrace);
         } else {
            String message = "Success : " + okTrace + ". Failures : " + failureTrace;
            log.error(message);
            System.out.println(message);
            throw new RuntimeException(message);
         }
      } finally {
         if (retval != null) {
            try {
               retval.close();
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
View Full Code Here

Examples of java.net.MulticastSocket

        // Validate and update our current component state
        if (started)
            throw new LifecycleException(sm.getString("standardCluster.alreadyStarted"));

        try {
            multicastSocket = new MulticastSocket(multicastPort);

            if(multicastSocket != null && multicastAddress != null) {
                multicastSocket.joinGroup(multicastAddress);

                clusterSender = getClusterSender(getName());
View Full Code Here

Examples of java.net.MulticastSocket

        int port,
        InetAddress bind,
        InetAddress mcastAddress,
        MembershipListener service)
    throws IOException {
        if ( bind != null) socket = new MulticastSocket(new java.net.InetSocketAddress(bind,port));
        else socket = new MulticastSocket(port);
        this.member = member;
        address = mcastAddress;
        this.port = port;
        sendPacket = new DatagramPacket(new byte[1000],1000);
        sendPacket.setAddress(address);
View Full Code Here

Examples of java.net.MulticastSocket

        this.service = service;
        this.sendFrequency = sendFrequency;
    }
   
    protected void setupSocket() throws IOException {
        if (mcastBindAddress != null) socket = new MulticastSocket(new java.net.
            InetSocketAddress(mcastBindAddress, port));
        else socket = new MulticastSocket(port);
        if (mcastBindAddress != null) {
      if(log.isInfoEnabled())
                log.info("Setting multihome multicast interface to:" +
                         mcastBindAddress);
            socket.setInterface(mcastBindAddress);
View Full Code Here

Examples of java.net.MulticastSocket

     */
    public void test2276() throws IOException {
        final String ADDRESS = "239.255.2.3";
        final int PORT = Support_PortManager.getNextPortForUDP();
        InetAddress group = InetAddress.getByName(ADDRESS);
        MulticastSocket socket = new MulticastSocket(PORT);
        byte[] recvData = new byte[100];
        DatagramPacket recvDatagram = new DatagramPacket(recvData, recvData.length);       

        String message = "Hello, world!";
        String longerMessage = message + " again.";
        String veryLongMessage = longerMessage + " Forever!";

        socket.joinGroup(group);
        socket.setSoTimeout(5000); // prevent eternal block in
                                   // socket.receive()
        // send & recieve packet
        byte[] sendData = message.getBytes();
        DatagramPacket sendDatagram = new DatagramPacket(sendData, 0,
                sendData.length, group, PORT);
        socket.send(sendDatagram);
        socket.receive(recvDatagram);
        String recvMessage = new String(recvData, 0, recvDatagram.getLength());
        assertEquals(message, recvMessage);
       
        // send & receive longer packet
        sendData = longerMessage.getBytes();
        sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
                group, PORT);
        socket.send(sendDatagram);
        socket.receive(recvDatagram);
        recvMessage = new String(recvData, 0, recvDatagram.getLength());
        assertEquals(longerMessage, recvMessage);

        // tricky case, added to test compatibility with RI;
        // depends on the previous test case
        sendData = veryLongMessage.getBytes();
        sendDatagram = new DatagramPacket(sendData, 0, sendData.length, group,
                PORT);
        socket.send(sendDatagram);
        recvDatagram.setLength(recvDatagram.getLength()); // !!!
        socket.receive(recvDatagram);
        recvMessage = new String(recvData, 0, recvDatagram.getLength());
        assertEquals(longerMessage, recvMessage);

        // tests if received packet is truncated after length was set to 1
        sendData = message.getBytes();
        sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
                group, PORT);
        socket.send(sendDatagram);
        recvDatagram.setLength(1);
        socket.receive(recvDatagram);
        assertEquals("Received message was not truncated", 1,
                recvDatagram.getLength());
        assertSame("Received message is invalid", sendData[0], recvData[0]);

        socket.leaveGroup(group);
        socket.close();
    }
View Full Code Here

Examples of java.net.MulticastSocket

  /**
   * @tests java.net.MulticastSocket#MulticastSocket()
   */
  public void test_Constructor() throws IOException {
    // regression test for 497
        MulticastSocket s = new MulticastSocket();
        // regression test for Harmony-1162
        assertTrue(s.getReuseAddress());
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.