Example usage for java.net MulticastSocket close

List of usage examples for java.net MulticastSocket close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes this datagram socket.

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 w  w .j av  a2  s  .  c om*/
    }
    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 .  ja v  a 2s .  c  om
    }
    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;

    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 . jav  a2  s  .  co  m
    }
    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);/*www  .  j av  a 2 s  . c  o m*/
    }
    ms.leaveGroup(InetSocketAddress.createUnresolved("java2s.com", 8080), NetworkInterface.getByIndex(0));

    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. ja  va 2  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: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);/* w ww . ja v a  2s .co 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:net.sbbi.upnp.Discovery.java

/**
 * Sends an SSDP search message on the network
 * @param src the sender ip/* w  w  w.ja va  2 s . c o  m*/
 * @param ttl the time to live
 * @param mx the mx field
 * @param searchTarget the search target
 * @throws IOException if some IO errors occurs during search
 */
public static void sendSearchMessage(InetAddress src, int ttl, int mx, String searchTarget) throws IOException {

    int bindPort = DEFAULT_SSDP_SEARCH_PORT;
    String port = System.getProperty("net.sbbi.upnp.Discovery.bindPort");
    if (port != null) {
        bindPort = Integer.parseInt(port);
    }
    InetSocketAddress adr = new InetSocketAddress(InetAddress.getByName(Discovery.SSDP_IP),
            Discovery.SSDP_PORT);

    java.net.MulticastSocket skt = new java.net.MulticastSocket(null);
    skt.bind(new InetSocketAddress(src, bindPort));
    skt.setTimeToLive(ttl);
    StringBuffer packet = new StringBuffer();
    packet.append("M-SEARCH * HTTP/1.1\r\n");
    packet.append("HOST: 239.255.255.250:1900\r\n");
    packet.append("MAN: \"ssdp:discover\"\r\n");
    packet.append("MX: ").append(mx).append("\r\n");
    packet.append("ST: ").append(searchTarget).append("\r\n").append("\r\n");
    if (log.isDebugEnabled())
        log.debug("Sending discovery message on 239.255.255.250:1900 multicast address form ip "
                + src.getHostAddress() + ":\n" + packet.toString());
    String toSend = packet.toString();
    byte[] pk = toSend.getBytes();
    skt.send(new DatagramPacket(pk, pk.length, adr));
    skt.disconnect();
    skt.close();
}

From source file:net.pms.network.UPNPHelper.java

/**
 * Send the UPnP BYEBYE message.// ww  w .  j a v a2 s  .c o  m
 */
public static void sendByeBye() {
    logger.info("Sending BYEBYE...");

    MulticastSocket multicastSocket = null;

    try {
        multicastSocket = getNewMulticastSocket();
        InetAddress upnpAddress = getUPNPAddress();
        multicastSocket.joinGroup(upnpAddress);

        sendMessage(multicastSocket, "upnp:rootdevice", BYEBYE);
        sendMessage(multicastSocket, "urn:schemas-upnp-org:device:MediaServer:1", BYEBYE);
        sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ContentDirectory:1", BYEBYE);
        sendMessage(multicastSocket, "urn:schemas-upnp-org:service:ConnectionManager:1", BYEBYE);
    } catch (IOException e) {
        logger.debug("Error sending BYEBYE message", e);
    } finally {
        if (multicastSocket != null) {
            // Clean up the multicast socket nicely
            try {
                InetAddress upnpAddress = getUPNPAddress();
                multicastSocket.leaveGroup(upnpAddress);
            } catch (IOException e) {
            }

            multicastSocket.disconnect();
            multicastSocket.close();
        }
    }
}

From source file:com.all.landownloader.discovery.LanDiscoverySocket.java

public void announceDeath() throws IllegalArgumentException {
    try {//from  w  w  w .  j  av  a  2s  .c o m
        byte[] buf = createPacket(BYE_MSG);
        DatagramPacket packet = new DatagramPacket(buf, buf.length, addressGroup, PORT);
        MulticastSocket socket = new MulticastSocket();
        socket.send(packet);
        packet = new DatagramPacket(buf, buf.length, broadcast, PORT);
        socket.send(packet);
        socket.close();
    } catch (IOException e) {
    }
}

From source file:com.all.landownloader.discovery.LanDiscoverySocket.java

private void announce() throws IllegalArgumentException {
    try {/*from   w ww .j a v a 2  s  . com*/
        byte[] buf = createPacket(ANNOUNCE_MSG);
        DatagramPacket packet = new DatagramPacket(buf, buf.length, addressGroup, PORT);
        MulticastSocket socket = new MulticastSocket();
        socket.send(packet);
        packet = new DatagramPacket(buf, buf.length, broadcast, PORT);
        socket.send(packet);
        socket.close();
    } catch (Exception e) {
    }

}