Example usage for java.net MulticastSocket MulticastSocket

List of usage examples for java.net MulticastSocket MulticastSocket

Introduction

In this page you can find the example usage for java.net MulticastSocket MulticastSocket.

Prototype

public MulticastSocket(SocketAddress bindaddr) throws IOException 

Source Link

Document

Create a MulticastSocket bound to the specified socket address.

Usage

From source file:ca.viaware.dlna.ssdp.SSDPService.java

public SSDPService() {
    ssdpConfig = SettingsManager.getServerConfig().getJSONObject("ssdp");
    httpConfig = SettingsManager.getServerConfig().getJSONObject("http");

    try {/*  w w w .j  a  va  2 s  .c  o m*/
        outSock = new MulticastSocket(ssdpConfig.getInt("port"));
        outSock.setNetworkInterface(
                NetworkInterface.getByInetAddress(InetAddress.getByName(ssdpConfig.getString("host"))));
        Log.info("SSDP: Outbound UDP bound to " + this.outSock.getLocalAddress());
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.baseAnnounce = "";
    this.baseAnnounce += "HOST: 239.255.255.250:1900\r\n";
    this.baseAnnounce += "CACHE-CONTROL: max-age=1800\r\n";
    this.baseAnnounce += "NTS: ssdp:alive\r\n";
    this.baseAnnounce += "SERVER:" + Globals.SERVER + "\r\n";
}

From source file:org.nuxeo.runtime.detection.MulticastDetector.java

public MulticastDetector(String identity, InetAddress groupAddr, int groupPort) throws IOException {
    this.identity = identity;
    this.groupAddr = groupAddr;
    this.groupPort = groupPort;
    socket = new MulticastSocket(groupPort);
    peers = new HashMap<String, Peer<T>>();
}

From source file:com.chiralBehaviors.slp.hive.configuration.BroadcastConfiguration.java

@Override
public Engine construct() throws IOException {
    Tuple<NetworkInterface, InetSocketAddress> tuple = getNetworkInterface();
    DatagramSocket socket = new MulticastSocket(new InetSocketAddress(port));
    socket.setReuseAddress(true);//w w w  . j  a  va2  s .co  m
    socket.setBroadcast(true);
    return new MulticastEngine(getFdFactory(), Generators.timeBasedGenerator(), heartbeatPeriod, heartbeatUnit,
            socket, tuple.b, receiveBufferMultiplier, sendBufferMultiplier, getMac(), tuple.a);
}

From source file:org.vesalainen.nmea.processor.SNTPMulticaster.java

public SNTPMulticaster(SntpMulticasterType sntpMulticasterType)
        throws SocketException, UnknownHostException, IOException {
    log.setLogger(this.getClass());
    int pollInterval = 6;
    Integer interval = sntpMulticasterType.getPollInterval();
    if (interval != null) {
        pollInterval = interval;/*from www . j  av a2  s  .  c o m*/
    }
    period = (long) Math.pow(2, pollInterval) * 1000;
    socket = new MulticastSocket(NTP_PORT);
    group = InetAddress.getByName("224.0.0.4");
    socket.joinGroup(group);
    ntpMessage = new NtpV4Impl();
    ntpMessage.setMode(MODE_BROADCAST);
    ntpMessage.setPoll(pollInterval);
    ntpMessage.setPrecision(-20);
    ntpMessage.setStratum(1);
    ntpMessage.setReferenceId(ReferenceIdentifier.GPS);

}

From source file:org.squidy.manager.protocol.multicast.MulticastServer.java

public MulticastServer(InetAddress group, int port) {
    try {/*www. ja  v  a2 s. co  m*/
        this.group = group;

        socket = new MulticastSocket(port);
        socket.joinGroup(group);
        start();
        LOG.info("Multicast server started on port " + port);
    } catch (IOException e) {
        LOG.error("Couldn't start multicast server on port " + port);
    }

}

From source file:org.apache.jcs.auxiliary.lateral.socket.udp.LateralUDPReceiver.java

/**
 * Constructor for the LateralUDPReceiver object
 *
 * @param multicastAddressString/*from w w w .ja v  a 2  s .  c  om*/
 * @param multicastPort
 * @exception IOException
 */
protected LateralUDPReceiver(String multicastAddressString, int multicastPort) throws IOException {

    log.debug("constructing listener, " + multicastAddressString + ":" + multicastPort);

    try {
        m_socket = new MulticastSocket(multicastPort);
        m_socket.joinGroup(InetAddress.getByName(multicastAddressString));
    } catch (IOException e) {
        log.error(e);
        log.debug("Could not bind to multicast address " + multicastAddressString + ":" + multicastPort);
        //throw e ;//new CacheException( "Could not bind to multicast address " + multicastAddressString + ":" + multicastPort, e);
    }
}

From source file:fi.tut.fast.MulticastConsumer.java

@Override
protected void doStart() throws Exception {
    super.doStart();

    //       System.out.println(endpoint.getEndpointUri());
    //      System.out.println("Starting Comsumer Endpoint on interface " + endpoint.getInterface().getDisplayName());

    LOG.info(String.format("Starting Comsumer Endpoint on interface %s",
            endpoint.getInterface().getDisplayName()));

    messageQueue = new MessageQueue();
    s = new MulticastSocket(endpoint.getMulticastAddress().getPort());
    s.setReuseAddress(true);//from  w  w w .  j  a v  a2 s .  co  m
    s.setNetworkInterface(endpoint.getInterface());
    s.joinGroup(InetAddress.getByName(endpoint.getMulticastAddress().getHost()));
    s.setSoTimeout(endpoint.getSocketTimeout());

    dlThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!done) {
                try {
                    consume();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                s.leaveGroup(InetAddress.getByName(endpoint.getMulticastAddress().getHost()));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            s.close();

        }

    });
    dlThread.setDaemon(true);
    dlThread.start();

}

From source file:fi.tut.fast.MulticastProducer.java

@Override
protected void doStart() throws Exception {
    super.doStart();

    InetAddress addr = null;//  w  w w  .  j  av a2s.c  o  m
    Class addrClass;
    if (endpoint.getMulticastGroup() instanceof Inet4Address) {
        addrClass = Inet4Address.class;
    } else {
        addrClass = Inet6Address.class;
    }

    for (Enumeration<InetAddress> as = endpoint.getInterface().getInetAddresses(); as.hasMoreElements();) {
        InetAddress a = as.nextElement();
        if (addrClass.isInstance(a)) {
            addr = a;
        }
    }

    if (addr == null) {
        addr = endpoint.getInterface().getInetAddresses().nextElement();
    }

    LOG.info(String.format("Starting Producer Endpoint on interface %s (%s) ",
            endpoint.getInterface().getDisplayName(), addr.getHostAddress()));

    s = new MulticastSocket(new InetSocketAddress(addr.getHostAddress(), endpoint.getSourcePort()));
    s.setReuseAddress(true);
    s.setNetworkInterface(endpoint.getInterface());
    s.setLoopbackMode(true);
    //      s.setBroadcast(true);

    //      s.joinGroup(endpoint.getMulticastGroup());
    //      s.connect(endpoint.getMulticastGroup(), endpoint.getAddress().getPort());
    //      s.bind(new InetSocketAddress(endpoint.getMulticastGroup(), 0));
}

From source file:net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver.java

/**
 * Start./*www . ja v  a 2  s.  com*/
 *
 * @throws IOException
 */
final void init() throws IOException {
    socket = new MulticastSocket(groupMulticastPort.intValue());
    socket.joinGroup(groupMulticastAddress);
    receiverThread = new MulticastReceiverThread();
    receiverThread.start();
    processingThreadPool = Executors.newCachedThreadPool();
}

From source file:org.nebulaframework.discovery.multicast.MulticastDiscovery.java

/**
 * Starts Multicast Discovery Service. This can only
 * be invoked by a Nebula ClusterManager.
 * //w w  w . j  a  va 2  s .co m
 * @throws IOException if occurred during operation
 * @throws UnsupportedOperationException if invoked by non-ClusterManager nodes.
 */
public static void startService() throws IOException, UnsupportedOperationException {

    // Only allowed for ClusterManagers
    if (!Grid.isClusterManager()) {
        throw new UnsupportedOperationException(
                "Multicast Discovery Service can be enabled only for ClusterManagers");
    }

    // Start Service
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {

                // Start Multicast Socket and listen for Requests
                final MulticastSocket mSock = new MulticastSocket(SERVICE_PORT);
                mSock.joinGroup(SERVICE_REQUEST_IP);

                // Infinite Loop
                while (true) {
                    // Buffer (for Greeting Message)
                    byte[] msg = new byte[GREET_MSG.getBytes("UTF-8").length];

                    // Create Datagram Packet
                    DatagramPacket packet = new DatagramPacket(msg, msg.length);

                    // Wait and Receive Request
                    mSock.receive(packet);
                    log.debug("[MulticastDiscovery] Received Discovery Request");

                    // Check if Greeting Message is valid
                    try {
                        String greet = new String(packet.getData());
                        if (!greet.equals(GREET_MSG)) {
                            throw new IllegalArgumentException("Invalid Greeting");
                        }
                    } catch (Exception e) {
                        log.debug("Malformed Multicast Message Igonored");
                        continue;
                    }

                    // Respond
                    doRespond();
                }

            } catch (IOException e) {
                log.error("[MulticastDiscovery] Service Failed on Receive", e);
            }

        }

    });
    t.setDaemon(true); // Run as Daemon thread
    t.start(); // Start Service
    log.debug("[MulticastDiscovery] Service Started");
}