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[] 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 ww. j a v  a 2  s .co  m
    }
    ms.setInterface(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;

    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 w w w  .ja v a  2s .com
    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);//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.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  av  a2s .  c  om*/
    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);/*from  www.  j  a  v a2 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:MulticastSniffer.java

public static void main(String[] args) {

    InetAddress ia = null;//from   w  w w .  java  2  s . c  o m
    byte[] buffer = new byte[65509];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //
        }
        port = Integer.parseInt(args[1]);
    } // end try
    catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer MulticastAddress port");
        System.exit(1);
    }

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            ms.receive(dp);
            String s = new String(dp.getData(), 0, 0, dp.getLength());
            System.out.println(s);
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }

}

From source file:MulticastSniffer.java

public static void main(String[] args) {
    InetAddress ia = null;/*from w  ww  . j a v  a 2s.  c  o m*/
    byte[] buffer = new byte[65535];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            System.err.println(e);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer mcast-addr port");
        System.exit(1);
    }
    System.out.println("About to join " + ia);

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            System.out.println("About to receive...");
            ms.receive(dp);
            String s = new String(dp.getData(), 0, 0, dp.getLength());
            System.out.println(s);
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }
}

From source file:MulticastClient.java

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

   MulticastSocket socket = new MulticastSocket(4446);
   InetAddress address = InetAddress.getByName("230.0.0.1");
   socket.joinGroup(address);//ww  w  .  j a  v  a 2  s . c o m

   DatagramPacket packet;

   // get a few quotes
   for (int i = 0; i < 5; i++) {

      byte[] buf = new byte[256];
      packet = new DatagramPacket(buf, buf.length);
      socket.receive(packet);

      String received = new String(packet.getData());
      System.out.println("Quote of the Moment: " + received);
   }

   socket.leaveGroup(address);
   socket.close();
}

From source file:Main.java

static void sendData(byte[] data) throws IOException {
    Log.d(TAG, "sendData data.len:" + data.length);
    while (true) {

        if (multicastSocket == null) {
            multicastSocket = new MulticastSocket(MULTICAST_PORT);
            Log.d(TAG, "new MulticastSocket()");
        }//from  w w w  .  java 2 s .  c om

        //MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
        multicastSocket.setLoopbackMode(true);
        InetAddress group;
        DatagramPacket packet;
        for (int i = 0; i < data.length; i++) {
            Log.d(TAG, i + ":" + data[i]);
            group = InetAddress.getByName("239." + i + "." + data[i] + ".254");

            multicastSocket.joinGroup(group);
            packet = new DatagramPacket("".getBytes(), "".getBytes().length, group, MULTICAST_PORT);
            multicastSocket.send(packet);
            multicastSocket.leaveGroup(group);
        }
    }
}

From source file:com.liferay.util.transport.MulticastTransport.java

public synchronized void connect() throws IOException {
    if (_socket == null) {
        _socket = new MulticastSocket(_port);
    } else if (_socket.isConnected() && _socket.isBound()) {
        return;// ww  w .  j a va  2 s. co  m
    }

    _address = InetAddress.getByName(_host);

    _socket.joinGroup(_address);

    _connected = true;

    start();
}