Search in sources :

Example 36 with MulticastSocket

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

the class MulticastReceiverTest method createSocket.

@Override
protected DatagramSocket createSocket() throws IOException {
    MulticastSocket socket = new MulticastSocket(Network.DEFAULT_PORT + 100);
    String laddr = Network.DEFAULT_V4_ADDR;
    getReceiver().setListenAddress(laddr);
    socket.joinGroup(InetAddress.getByName(laddr));
    return socket;
}
Also used : MulticastSocket(java.net.MulticastSocket)

Example 37 with MulticastSocket

use of java.net.MulticastSocket in project openhab1-addons by openhab.

the class SsdpDiscovery method sendDiscoveryBroacast.

/**
     * Broadcasts a SSDP discovery message into the network to find provided
     * services.
     * 
     * @return The Socket the answers will arrive at.
     * @throws UnknownHostException
     * @throws IOException
     * @throws SocketException
     * @throws UnsupportedEncodingException
     */
private MulticastSocket sendDiscoveryBroacast() throws UnknownHostException, IOException, SocketException, UnsupportedEncodingException {
    InetAddress multicastAddress = InetAddress.getByName("239.255.255.250");
    final int port = 1900;
    MulticastSocket socket = new MulticastSocket(port);
    socket.setReuseAddress(true);
    socket.setSoTimeout(130000);
    socket.joinGroup(multicastAddress);
    byte[] requestMessage = DISCOVER_MESSAGE.getBytes("UTF-8");
    DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, multicastAddress, port);
    socket.send(datagramPacket);
    return socket;
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Example 38 with MulticastSocket

use of java.net.MulticastSocket in project openhab1-addons by openhab.

the class SsdpDiscovery method findIpForResponseKeywords.

/**
     * Determines the IP address of a service provided in the network. The
     * service is detected by one or more keywords that have to appear in the
     * answer message of the service to the SSDP discover broadcast. The
     * keywords are not case sensitive. The timeout of the discovery is 120
     * seconds.
     * 
     * @param keywords
     *            The keywords that have to be found case insensitive in the
     *            response of the service to be searched.
     * @return The IP if a service with the keywords has been found, null
     *         otherwise.
     * @throws IOException
     */
public String findIpForResponseKeywords(String... keywords) throws IOException {
    try {
        logger.debug("Sending SSDP discover.");
        MulticastSocket socket = sendDiscoveryBroacast();
        logger.debug("Waiting for response.");
        return scanResposesForKeywords(socket, keywords);
    } catch (SocketTimeoutException e) {
        logger.debug("Timeout of request...");
    }
    return null;
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketTimeoutException(java.net.SocketTimeoutException)

Example 39 with MulticastSocket

use of java.net.MulticastSocket in project CloudStack-archive by CloudStack-extras.

the class MultiCaster method start.

public void start(String strOutboundAddress, String strClusterAddress, int nPort) throws SocketException {
    assert (socket == null);
    InetAddress addr = null;
    try {
        addr = InetAddress.getByName(strClusterAddress);
    } catch (IOException e) {
        s_logger.error("Unexpected exception ", e);
    }
    if (addr != null && addr.isMulticastAddress()) {
        try {
            socket = new MulticastSocket(nPort);
            socket.setReuseAddress(true);
            if (s_logger.isInfoEnabled())
                s_logger.info("Join multicast group : " + addr);
            ((MulticastSocket) socket).joinGroup(addr);
            ((MulticastSocket) socket).setTimeToLive(1);
            if (strOutboundAddress != null) {
                if (s_logger.isInfoEnabled())
                    s_logger.info("set outgoing interface to : " + strOutboundAddress);
                InetAddress ia = InetAddress.getByName(strOutboundAddress);
                NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
                ((MulticastSocket) socket).setNetworkInterface(ni);
            }
        } catch (IOException e) {
            s_logger.error("Unexpected exception ", e);
        }
    } else {
        socket = new DatagramSocket(nPort);
        socket.setReuseAddress(true);
    }
    driver = new Thread(this, "Multi-caster");
    driver.setDaemon(true);
    driver.start();
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramSocket(java.net.DatagramSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 40 with MulticastSocket

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

the class TestGangliaSink method testShouldCreateMulticastSocket.

@Test
public void testShouldCreateMulticastSocket() throws Exception {
    SubsetConfiguration conf = new ConfigBuilder().add("test.sink.ganglia.multicast", true).subset("test.sink.ganglia");
    GangliaSink30 gangliaSink = new GangliaSink30();
    gangliaSink.init(conf);
    DatagramSocket socket = gangliaSink.getDatagramSocket();
    assertTrue("Did not create MulticastSocket", socket != null && socket instanceof MulticastSocket);
    int ttl = ((MulticastSocket) socket).getTimeToLive();
    assertEquals("Did not set default TTL", 1, ttl);
}
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)

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