Example usage for java.net MulticastSocket leaveGroup

List of usage examples for java.net MulticastSocket leaveGroup

Introduction

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

Prototype

public void leaveGroup(InetAddress mcastaddr) throws IOException 

Source Link

Document

Leave a multicast group.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket();
    ms.joinGroup(ia);/*from   w  w w . ja va2  s .  c om*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);
    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket();
    ms.joinGroup(ia);//from   w w  w  . j a va 2 s  .com
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    ia = ms.getInterface();
    System.out.println(ia);

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(8080);
    ms.joinGroup(ia);/*from   w  w w. ja v a 2s. c o m*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    ia = ms.getInterface();
    System.out.println(ia);

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);//w  ww  . ja va 2 s  . co  m
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getTimeToLive());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);//w  w  w.j  a v a2  s.c  om
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getLoopbackMode());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);/*from  ww  w .j  a  v a 2 s. c o  m*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getNetworkInterface().getDisplayName());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);//  w  ww .j ava  2  s .com
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    ia = ms.getInterface();
    System.out.println(ia);

    ms.close();
}

From source file:MulticastSender.java

public static void main(String[] args) {

    InetAddress ia = null;//from  w w w . j  ava2  s .c  om
    int port = 0;
    String characters = "Here's some multicast data\n";
    byte[] data = new byte[characters.length()];

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //ia = InetAddressFactory.newInetAddress(args[0]);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSender MulticastAddress port");
        System.exit(1);
    }

    characters.getBytes(0, characters.length(), data, 0);
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    try {
        MulticastSocket ms = new MulticastSocket();
        ms.joinGroup(ia);
        for (int i = 1; i < 10; i++) {
            ms.send(dp, (byte) 1);
        }
        ms.leaveGroup(ia);
        ms.close();
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int port = 0;
    byte ttl = (byte) 1;

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName("google.com"), port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("google.com", 8080));
    ms.joinGroup(InetSocketAddress.createUnresolved("127.0.0.1", 8080), NetworkInterface.getByIndex(0));
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);/*w  w w.  java  2s.c  om*/
    }
    ms.leaveGroup(InetAddress.getByName("google.com"));

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    MulticastSocket mcSocket = null;
    InetAddress mcIPAddress = null;
    mcIPAddress = InetAddress.getByName(mcIPStr);
    mcSocket = new MulticastSocket(mcPort);
    System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress());
    mcSocket.joinGroup(mcIPAddress);/*www  . j  ava2 s .c  om*/

    DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);

    System.out.println("Waiting for a  multicast message...");
    mcSocket.receive(packet);
    String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());
    System.out.println("[Multicast  Receiver] Received:" + msg);

    mcSocket.leaveGroup(mcIPAddress);
    mcSocket.close();
}