Examples of MulticastSocket


Examples of java.net.MulticastSocket

    {
        if (!(channel.socket() instanceof MulticastSocket)) {
            throw new UnsupportedOperationException("not a multicast socket");
        }

        MulticastSocket msocket = ((MulticastSocket) channel.socket());

        if (group == null) {
            throw new NullPointerException("null multicast group");
        }

        if (interf == null) {
            throw new NullPointerException("null interface");
        }

        if (!group.isMulticastAddress()) {
            throw new UnsupportedAddressTypeException();
        }

        if (protocolFamily == StandardProtocolFamily.INET) {
            if (!((group instanceof Inet4Address) ||
                   ((group instanceof Inet6Address) &&
                       ((Inet6Address) group).isIPv4CompatibleAddress()))) {
                throw new UnsupportedAddressTypeException();
            }
        } else if (protocolFamily == StandardProtocolFamily.INET6) {
            if (!(group instanceof Inet6Address)) {
                throw new UnsupportedAddressTypeException();
            }
        }

        InetSocketAddress mcastaddr = new InetSocketAddress(group, 0);
        MembershipKeyImpl newKey = new MembershipKeyImpl(mcastaddr, interf);

        MembershipKeyImpl existingKey =
            mcastKeys.putIfAbsent(newKey, newKey);

        if (existingKey != null) {
            return existingKey;
        }

        boolean success = false;
        try {
            msocket.joinGroup(mcastaddr, interf);
            success = true;
            return newKey;
        } finally {
            if (!success) {
                mcastKeys.remove(newKey);
View Full Code Here

Examples of java.net.MulticastSocket

         * {@inheritDoc}
         */
        @Override
        public void drop() throws IOException {
            mcastKeys.remove(this);
            MulticastSocket msocket = (MulticastSocket) channel.socket();
            msocket.leaveGroup(mcastaddr, netIf);
        }
View Full Code Here

Examples of java.net.MulticastSocket

   
    protected void setupSocket() throws IOException {
        if (mcastBindAddress != null) {
            try {
                log.info("Attempting to bind the multicast socket to "+address+":"+port);
                socket = new MulticastSocket(new InetSocketAddress(address,port));
            } catch (BindException e) {
                /*
                 * On some plattforms (e.g. Linux) it is not possible to bind
                 * to the multicast address. In this case only bind to the
                 * port.
                 */
                log.info("Binding to multicast address, failed. Binding to port only.");
                socket = new MulticastSocket(port);
            }
        } else {
            socket = new MulticastSocket(port);
        }
        socket.setLoopbackMode(false);
        if (mcastBindAddress != null) {
      if(log.isInfoEnabled())
                log.info("Setting multihome multicast interface to:" +mcastBindAddress);
View Full Code Here

Examples of java.net.MulticastSocket

                InetAddress inetAddress = InetAddress.getByName(host);

                this.address = new InetSocketAddress(inetAddress, port);

                multicast = new MulticastSocket(port);
                multicast.setLoopbackMode(loopbackMode);
                multicast.setTimeToLive(timeToLive);
                multicast.joinGroup(inetAddress);
                multicast.setSoTimeout((int) heartRate);
View Full Code Here

Examples of java.net.MulticastSocket

    }

    try {
      // First, close the previous connection if any.
      cleanUp();
      outSocket = new MulticastSocket();
      outSocket.setTimeToLive(timeToLive);
    } catch (IOException e) {
      getLogger().error("Error in connect method of MulticastAppender named "+name, e);
    }
  }
View Full Code Here

Examples of java.net.MulticastSocket

        // } catch (Exception exception) {
        // logger.log(Level.WARNING, "openMulticastSocket() Open socket exception Address: " + address + ", ", exception);
        // // The most likely cause is a duplicate address lets open without specifying the address
        // _socket = new MulticastSocket(DNSConstants.MDNS_PORT);
        // }
        _socket = new MulticastSocket(DNSConstants.MDNS_PORT);
        if ((hostInfo != null) && (hostInfo.getInterface() != null)) {
            try {
                _socket.setNetworkInterface(hostInfo.getInterface());
            } catch (SocketException e) {
                if (logger.isLoggable(Level.FINE)) {
View Full Code Here

Examples of java.net.MulticastSocket

                    }
                } catch (final IOException e) {
                    logger.throwing(getClass().toString(), "send(" + this.getName() + ") - JmDNS can not parse what it sends!!!", e);
                }
            }
            final MulticastSocket ms = _socket;
            if (ms != null && !ms.isClosed()) {
                ms.send(packet);
            }
        }
    }
View Full Code Here

Examples of java.net.MulticastSocket

    private final static int MPORT = 8053;

    @Test
    public void testTwoMulticastPortsAtOnce() throws UnknownHostException, IOException {
        System.out.println("Unit Test: testTwoMulticastPortsAtOnce()");
        MulticastSocket firstSocket = null;
        MulticastSocket secondSocket = null;
        try {
            String firstMessage = "ping";
            String secondMessage = "pong";
            InetAddress someInet = InetAddress.getByName(DNSConstants.MDNS_GROUP);
            firstSocket = new MulticastSocket(MPORT);
            secondSocket = new MulticastSocket(MPORT);

            firstSocket.joinGroup(someInet);
            secondSocket.joinGroup(someInet);
            //
            DatagramPacket out = new DatagramPacket(firstMessage.getBytes("UTF-8"), firstMessage.length(), someInet, MPORT);
            DatagramPacket inFirst = new DatagramPacket(firstMessage.getBytes("UTF-8"), firstMessage.length(), someInet, MPORT);
            DatagramPacket inSecond = new DatagramPacket(firstMessage.getBytes("UTF-8"), firstMessage.length(), someInet, MPORT);
            Receive receiveSecond = new Receive(secondSocket, inSecond);
            receiveSecond.start();
            Receive receiveFirst = new Receive(firstSocket, inSecond);
            receiveFirst.start();
            firstSocket.send(out);
            if (receiveSecond.waitForReceive()) {
                Assert.fail("We did not receive the data in the second socket");
            }
            String fromFirst = new String(inSecond.getData(), "UTF-8");
            assertEquals("Expected the second socket to recieve the same message the first socket sent", firstMessage, fromFirst);
            // Make sure the first socket had read its own message
            if (receiveSecond.waitForReceive()) {
                Assert.fail("We did not receive the data in the first socket");
            }
            // Reverse the roles
            out = new DatagramPacket(secondMessage.getBytes("UTF-8"), secondMessage.length(), someInet, MPORT);
            inFirst = new DatagramPacket(secondMessage.getBytes("UTF-8"), secondMessage.length(), someInet, MPORT);
            receiveFirst = new Receive(firstSocket, inSecond);
            receiveFirst.start();

            secondSocket.send(out);
            if (receiveFirst.waitForReceive()) {
                Assert.fail("We did not receive the data in the first socket");
            }
            String fromSecond = new String(inFirst.getData(), "UTF-8");
            assertEquals("Expected the first socket to recieve the same message the second socket sent", secondMessage, fromSecond);
        } finally {
            if (firstSocket != null) firstSocket.close();
            if (secondSocket != null) secondSocket.close();
        }
    }
View Full Code Here

Examples of java.net.MulticastSocket

        }
        logger.info(msg);

        try {
            this.group = InetAddress.getByName(address);
            this.sock = new MulticastSocket(port);
            this.sock.setSoTimeout(timeout);

            if (disableLoopback){
                sock.setLoopbackMode(disableLoopback);
            }
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
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.