Search in sources :

Example 1 with MulticastSocket

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

the class TestGangliaSink method testShouldCreateDatagramSocketByDefault.

@Test
public void testShouldCreateDatagramSocketByDefault() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder().subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Test(org.junit.Test)

Example 2 with MulticastSocket

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

the class TestGangliaSink method testShouldCreateDatagramSocketIfMulticastIsDisabled.

@Test
public void testShouldCreateDatagramSocketIfMulticastIsDisabled() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder().add("test.sink.ganglia.multicast", false).subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertFalse("Did not create DatagramSocket", socket == null || socket instanceof MulticastSocket);
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) SubsetConfiguration(org.apache.commons.configuration2.SubsetConfiguration) Test(org.junit.Test)

Example 3 with MulticastSocket

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

the class MultiCastSender method send.

@Override
public int send(String mess) throws Exception {
    if (s == null) {
        try {
            group = InetAddress.getByName(config.getGroup());
            if (config.getHost() != null) {
                InetAddress addr = InetAddress.getByName(config.getHost());
                InetSocketAddress addrs = new InetSocketAddress(addr, config.getMultiport());
                s = new MulticastSocket(addrs);
            } else
                s = new MulticastSocket(config.getMultiport());
            s.setTimeToLive(config.getTtl());
            s.joinGroup(group);
        } catch (Exception ex) {
            log.error("Unable to use multicast: " + ex);
            s = null;
            return -1;
        }
    }
    byte[] buf;
    buf = mess.getBytes(StandardCharsets.US_ASCII);
    DatagramPacket data = new DatagramPacket(buf, buf.length, group, config.getMultiport());
    try {
        s.send(data);
    } catch (Exception ex) {
        log.error("Unable to send collected load information: " + ex);
        s.close();
        s = null;
        return -1;
    }
    return 0;
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Example 4 with MulticastSocket

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

the class UdpReceiver method getSocket.

public DatagramSocket getSocket() throws IOException {
    if (_socket == null) {
        if (_bindAddress == null) {
            _socket = new DatagramSocket(_port);
        } else {
            InetAddress addr = InetAddress.getByName(_bindAddress);
            if (addr.isMulticastAddress()) {
                MulticastSocket mcast = new MulticastSocket(_port);
                if (_ifAddress != null) {
                    mcast.setInterface(InetAddress.getByName(_ifAddress));
                }
                mcast.joinGroup(addr);
                _socket = mcast;
            } else {
                _socket = new DatagramSocket(_port, addr);
            }
        }
    }
    return _socket;
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) InetAddress(java.net.InetAddress)

Example 5 with MulticastSocket

use of java.net.MulticastSocket in project dubbo by alibaba.

the class MulticastRegistryTest method testDefaultPort.

@Test
public void testDefaultPort() {
    MulticastRegistry multicastRegistry = new MulticastRegistry(URL.valueOf("multicast://224.5.6.7"));
    try {
        MulticastSocket multicastSocket = multicastRegistry.getMutilcastSocket();
        Assert.assertEquals(1234, multicastSocket.getLocalPort());
    } finally {
        multicastRegistry.destroy();
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) Test(org.junit.Test)

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