Search in sources :

Example 26 with MulticastSocket

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

the class MulticastPulseAgent method getSockets.

private static MulticastSocket[] getSockets(final InetAddress ia, final int port) throws Exception {
    final ArrayList<MulticastSocket> list = new ArrayList<MulticastSocket>();
    for (final NetworkInterface ni : getInterfaces()) {
        MulticastSocket ms = null;
        try {
            ms = new MulticastSocket(port);
            ms.setNetworkInterface(ni);
            ms.setSoTimeout(0);
            ms.setTimeToLive(TTL);
            if (!ms.getBroadcast()) {
                ms.setBroadcast(true);
            }
            ms.joinGroup(ia);
            list.add(ms);
            log.debug(String.format("Created MulticastSocket for '%1$s:%2$s' on network adapter: %3$s", ia.getHostName(), port, ni));
        } catch (final Throwable e) {
            if (null != ms) {
                try {
                    ms.close();
                } catch (final Throwable t) {
                //Ignore
                }
            }
        }
    }
    return list.toArray(new MulticastSocket[list.size()]);
}
Also used : MulticastSocket(java.net.MulticastSocket) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface)

Example 27 with MulticastSocket

use of java.net.MulticastSocket in project ha-bridge by bwssytems.

the class SystemControl method pingListener.

protected void pingListener() {
    try {
        byte[] buf = new byte[256];
        String testData = "M-SEARCH * HTTP/1.1\nHOST: " + Configuration.UPNP_MULTICAST_ADDRESS + ":" + Configuration.UPNP_DISCOVERY_PORT + "ST: urn:schemas-upnp-org:device:CloudProxy:1\nMAN: \"ssdp:discover\"\nMX: 3";
        buf = testData.getBytes();
        MulticastSocket socket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
        InetAddress group = InetAddress.getByName(Configuration.UPNP_MULTICAST_ADDRESS);
        DatagramPacket packet;
        packet = new DatagramPacket(buf, buf.length, group, Configuration.UPNP_DISCOVERY_PORT);
        socket.send(packet);
        socket.close();
    } catch (IOException e) {
        log.warn("Error pinging listener. " + e.getMessage());
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 28 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 29 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 30 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)48 IOException (java.io.IOException)18 DatagramPacket (java.net.DatagramPacket)18 InetSocketAddress (java.net.InetSocketAddress)15 InetAddress (java.net.InetAddress)14 DatagramSocket (java.net.DatagramSocket)10 Test (org.junit.Test)10 SocketException (java.net.SocketException)7 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 NetworkInterface (java.net.NetworkInterface)4 SocketAddress (java.net.SocketAddress)4 SubsetConfiguration (org.apache.commons.configuration2.SubsetConfiguration)4 ConfigBuilder (org.apache.hadoop.metrics2.impl.ConfigBuilder)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 BindException (java.net.BindException)2