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:Main.java

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

    MulticastSocket msocket = new MulticastSocket(9999);
    byte[] inbuf = new byte[1024];
    DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

    msocket.receive(packet);/*  w  ww .  ja v a  2 s  . c o m*/

    // Data is now in inbuf
    int numBytesReceived = packet.getLength();
}

From source file:Main.java

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

    String groupName = "groupName";
    int port = 1024;
    MulticastSocket msocket = new MulticastSocket(port);
    InetAddress group = InetAddress.getByName(groupName);
    msocket.joinGroup(group);/*  w w w  .  j  a  v  a 2  s.c  o m*/

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int port = 0;
    InetAddress group = InetAddress.getByName("127.0.0.1");

    MulticastSocket ms = new MulticastSocket(port);
    ms.joinGroup(group);//ww  w .j  a v  a 2 s  .  c  o m

    byte[] buffer = new byte[8192];
    while (true) {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ms.receive(dp);
        String s = new String(dp.getData());
        System.out.println(s);
    }
    // ms.leaveGroup(group);
    // 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);//ww w  .  java2  s.c o  m

    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();
}

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);// w w  w . ja va 2 s.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;

    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);/*from w  w w.j av a  2s  .  c o m*/
    }
    ms.leaveGroup(InetAddress.getByName("google.com"));

    ms.close();
}

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  ww  .j  a v a  2s . c o m*/
    }
    ms.setLoopbackMode(true);

    ms.close();
}

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);// ww w  .  j a  v  a2 s .c  om
    }
    ms.setNetworkInterface(NetworkInterface.getByIndex(0));

    ms.close();
}

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);/*from   www  .  j  a va 2 s. com*/
    }
    ms.setTimeToLive(1000);

    ms.close();
}

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);//from   w w  w.  j av a  2 s.  c om
    }
    ms.leaveGroup(InetSocketAddress.createUnresolved("java2s.com", 8080), NetworkInterface.getByIndex(0));

    ms.close();
}