Java 8 MulticastSocket - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 MulticastSocket class.

This code example shows how to use the following methods:
    @Test 
    public void createMulticastSocket() throws IOException { 
 
        MulticastSocket socket1 = new MulticastSocket();
        MulticastSocket socket2 = new MulticastSocket();
        MulticastSocket socket3 = new MulticastSocket();
        SocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 1);
 
        when(this.factory.createMulticastSocket("test")).thenReturn(socket1);
        when(this.factory.createMulticastSocket("test", 1)).thenReturn(socket2);
        when(this.factory.createMulticastSocket("test", address)).thenReturn(socket3);
 
        MulticastSocket result1 = this.subject.createMulticastSocket("test");
        MulticastSocket result2 = this.subject.createMulticastSocket("test", 1);
        MulticastSocket result3 = this.subject.createMulticastSocket("test", address);
 
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket1);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket2);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket3);
 

8
CodeRank
This code example shows how to use the following methods:close
    public static void main(String[] args) throws Exception {
        MulticastSocket s1, s2;
 
        try { 
            s1 = new MulticastSocket(4160);
            s2 = new MulticastSocket(4160);
            s1.close();
            s2.close();
        } catch (BindException e) {
            throw new RuntimeException("MulticastSocket do no set SO_REUSEADDR");
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:isClosed, getNetworkInterface, joinGroup, setNetworkInterface, setTimeToLive
                if (mcSocket == null) {
                    mcSocket = new MulticastSocket(port);
                } 
 
                if (networkInterface != null) {
                    mcSocket.setNetworkInterface(networkInterface);
                } 
 
                mcSocket.joinGroup(address);
                mcSocket.setTimeToLive(64);
 
                if (!Tools.isAndroid()) { // Crashes in Android 2.3.3 emulator 
                    LOG.log(Level.FINE, "Connected to " + mcSocket.getNetworkInterface());
                } 
 
                connected = true;
            } 
        } 
 
        catch (final IOException e) {

6
CodeRank
This code example shows how to use the following methods:isBound, isConnected, joinGroup
 
	public synchronized void connect() throws IOException { 
		if (_socket == null) {
			_socket = new MulticastSocket(_port);
		} 
		else if (_socket.isConnected() && _socket.isBound()) {
			return; 
		} 
 
		_address = InetAddress.getByName(_host);
 
		_socket.joinGroup(_address);
 
		_connected = true;
 
		start();
	} 
 
	public synchronized void disconnect() { 
 
6
CodeRank
This code example shows how to use the following methods:close, send, joinGroup, setTimeToLive
                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 { 

6
CodeRank
This code example shows how to use the following methods:close, send, joinGroup, leaveGroup, setLoopbackMode
	 * @param message 
	 */ 
	public static void broadcast(String message) {
		try { 
			InetAddress address = InetAddress.getByName(JVM_MULTICAST);
			MulticastSocket socket = new MulticastSocket(MULTICASTSOCKET);
			socket.setTimeToLive(100);
			socket.setLoopbackMode(false);
			socket.joinGroup(address);
			byte[] data = null;
			data = message.getBytes();
			DatagramPacket packet = new DatagramPacket(data, data.length, address, JVM_PORT);
 
			// Sends the packet 
			try { 
				socket.send(packet);
			} catch (IOException exc) {
				exc.printStackTrace();
			} 
			// Stop the server 

6
CodeRank
This code example shows how to use the following methods:isClosed, getNetworkInterface, joinGroup, setNetworkInterface
                if (mcSocket == null) {
                    mcSocket = new MulticastSocket(port);
                } 
 
                if (networkInterface != null) {
                    mcSocket.setNetworkInterface(networkInterface);
                } 
 
                mcSocket.joinGroup(address);
 
                if (!Tools.isAndroid()) { // Crashes in Android 2.3.3 emulator 
                    LOG.log(Level.FINE, "Connected to " + mcSocket.getNetworkInterface());
                } 
 
                connected = true;
            } 
        } 
 
        catch (final IOException e) {
            LOG.log(Level.SEVERE, "Could not start receiver: " + e.toString(), e);

6
CodeRank
This code example shows how to use the following methods:getInetAddress, getLocalPort, joinGroup
 
    public MulticastConnection(SocketInetAddress address, MessageSerializer<T> serializer) {
        this.address = address;
        this.serializer = serializer;
        try { 
            socket = new MulticastSocket(address.getPort());
            socket.joinGroup(address.getAddress());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } 
        localAddress = new SocketInetAddress(socket.getInetAddress(), socket.getLocalPort());
    } 
 
    @Override 
    public String toString() {
        return String.format("multicast connection %s", address);
    } 
 
    public void dispatch(T message) {
        try { 
6
CodeRank
This code example shows how to use the following methods:sleep, close, send, setReceiveBufferSize, setSendBufferSize
        zenPingA.start();
 
        MulticastSocket multicastSocket = null;
        try { 
            Loggers.getLogger(MulticastZenPing.class).setLevel("TRACE"); 
            multicastSocket = new MulticastSocket(54328);
            multicastSocket.setReceiveBufferSize(2048);
            multicastSocket.setSendBufferSize(2048);
            multicastSocket.setSoTimeout(60000);
 
            DatagramPacket datagramPacket = new DatagramPacket(new byte[2048], 2048, InetAddress.getByName("224.2.2.4"), 54328);
            XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("request").field("cluster_name", "test").endObject().endObject();
            datagramPacket.setData(builder.bytes().toBytes());
            multicastSocket.send(datagramPacket);
            Thread.sleep(100);
        } finally { 
            Loggers.getLogger(MulticastZenPing.class).setLevel("INFO"); 
            if (multicastSocket != null) multicastSocket.close();
            zenPingA.close();
            threadPool.shutdown();

6
CodeRank
This code example shows how to use the following methods:joinGroup
    public DatagramSocket createDatagramSocket(int port, InetAddress laddr)
            throws SocketException { 
 
        if ( laddr.isMulticastAddress() ) {
            try { 
                MulticastSocket ds = new MulticastSocket( port );
                ds.joinGroup( laddr );
                return ds;
            } catch (IOException e) {
                throw new SocketException( e.getLocalizedMessage() );
            } 
        } else return new DatagramSocket(port, laddr);
    } 
 
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */ 
    public SSLServerSocket createSSLServerSocket(int port, int backlog,
            InetAddress bindAddress) throws IOException {
        return (SSLServerSocket) sslServerSocketFactory.createServerSocket(
                port, backlog, bindAddress);
    } 
5
CodeRank

Related Java 8 MulticastSocket Questions & Answers:

See Code Examples for Java 8 MulticastSocket Methods: