Search in sources :

Example 31 with MulticastSocket

use of java.net.MulticastSocket in project JAirPort by froks.

the class JmDNSImpl method send.

/**
     * Send an outgoing multicast DNS message.
     *
     * @param out
     * @exception IOException
     */
public void send(DNSOutgoing out) throws IOException {
    if (!out.isEmpty()) {
        byte[] message = out.data();
        final DatagramPacket packet = new DatagramPacket(message, message.length, _group, DNSConstants.MDNS_PORT);
        if (logger.isLoggable(Level.FINEST)) {
            try {
                final DNSIncoming msg = new DNSIncoming(packet);
                if (logger.isLoggable(Level.FINEST)) {
                    logger.finest("send(" + this.getName() + ") JmDNS out:" + msg.print(true));
                }
            } 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);
        }
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 32 with MulticastSocket

use of java.net.MulticastSocket in project hazelcast by hazelcast.

the class MulticastDiscoveryStrategy method initializeMulticastSocket.

private void initializeMulticastSocket() {
    try {
        int port = getOrDefault(MulticastProperties.PORT, DEFAULT_MULTICAST_PORT);
        PortValueValidator validator = new PortValueValidator();
        validator.validate(port);
        String group = getOrDefault(MulticastProperties.GROUP, DEFAULT_MULTICAST_GROUP);
        multicastSocket = new MulticastSocket(null);
        multicastSocket.bind(new InetSocketAddress(port));
        multicastSocket.setReuseAddress(true);
        multicastSocket.setTimeToLive(SOCKET_TIME_TO_LIVE);
        multicastSocket.setReceiveBufferSize(DATA_OUTPUT_BUFFER_SIZE);
        multicastSocket.setSendBufferSize(DATA_OUTPUT_BUFFER_SIZE);
        multicastSocket.setSoTimeout(SOCKET_TIMEOUT);
        multicastSocket.joinGroup(InetAddress.getByName(group));
        multicastDiscoverySender = new MulticastDiscoverySender(discoveryNode, multicastSocket, logger, group, port);
        multicastDiscoveryReceiver = new MulticastDiscoveryReceiver(multicastSocket, logger);
        if (discoveryNode != null) {
            isClient = false;
        }
    } catch (Exception e) {
        logger.finest(e.getMessage());
        rethrow(e);
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) MulticastDiscoverySender(com.hazelcast.spi.discovery.multicast.impl.MulticastDiscoverySender) MulticastDiscoveryReceiver(com.hazelcast.spi.discovery.multicast.impl.MulticastDiscoveryReceiver) InetSocketAddress(java.net.InetSocketAddress) ValidationException(com.hazelcast.config.properties.ValidationException) UnknownHostException(java.net.UnknownHostException)

Example 33 with MulticastSocket

use of java.net.MulticastSocket in project tomcat by apache.

the class McastServiceImpl method setupSocket.

protected void setupSocket() throws IOException {
    if (mcastBindAddress != null) {
        try {
            log.info(sm.getString("mcastServiceImpl.bind", address, Integer.toString(port)));
            socket = new MulticastSocket(new InetSocketAddress(address, port));
        } catch (BindException e) {
            /*
                 * On some platforms (e.g. Linux) it is not possible to bind
                 * to the multicast address. In this case only bind to the
                 * port.
                 */
            log.info(sm.getString("mcastServiceImpl.bind.failed"));
            socket = new MulticastSocket(port);
        }
    } else {
        socket = new MulticastSocket(port);
    }
    //hint if we want disable loop back(local machine) messages
    socket.setLoopbackMode(localLoopbackDisabled);
    if (mcastBindAddress != null) {
        if (log.isInfoEnabled())
            log.info(sm.getString("mcastServiceImpl.setInterface", mcastBindAddress));
        socket.setInterface(mcastBindAddress);
    }
    //force a so timeout so that we don't block forever
    if (mcastSoTimeout <= 0)
        mcastSoTimeout = (int) sendFrequency;
    if (log.isInfoEnabled()) {
        log.info(sm.getString("mcastServiceImpl.setSoTimeout", Integer.toString(mcastSoTimeout)));
    }
    socket.setSoTimeout(mcastSoTimeout);
    if (mcastTTL >= 0) {
        if (log.isInfoEnabled())
            log.info(sm.getString("mcastServiceImpl.setTTL", Integer.toString(mcastTTL)));
        socket.setTimeToLive(mcastTTL);
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException)

Example 34 with MulticastSocket

use of java.net.MulticastSocket in project jcollectd by collectd.

the class MBeanReceiver method setup.

private void setup() throws Exception {
    DatagramSocket socket = getSocket();
    if (socket instanceof MulticastSocket) {
        MulticastSocket mcast = (MulticastSocket) socket;
        System.err.println("Multicast interface=" + mcast.getInterface());
    }
    System.err.println(getListenAddress() + ":" + getPort() + " listening...");
    listen();
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket)

Example 35 with MulticastSocket

use of java.net.MulticastSocket in project jcollectd by collectd.

the class UdpSender method getMulticastSocket.

private MulticastSocket getMulticastSocket() throws IOException {
    if (_mcast == null) {
        _mcast = new MulticastSocket();
        _mcast.setTimeToLive(1);
    }
    return _mcast;
}
Also used : MulticastSocket(java.net.MulticastSocket)

Aggregations

MulticastSocket (java.net.MulticastSocket)50 IOException (java.io.IOException)19 DatagramPacket (java.net.DatagramPacket)19 InetSocketAddress (java.net.InetSocketAddress)16 InetAddress (java.net.InetAddress)15 Test (org.junit.Test)11 DatagramSocket (java.net.DatagramSocket)10 SocketException (java.net.SocketException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 UnknownHostException (java.net.UnknownHostException)6 NetworkInterface (java.net.NetworkInterface)5 SocketAddress (java.net.SocketAddress)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 URI (java.net.URI)4 SubsetConfiguration (org.apache.commons.configuration2.SubsetConfiguration)4 ConfigBuilder (org.apache.hadoop.metrics2.impl.ConfigBuilder)4 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 BindException (java.net.BindException)2