Java MulticastSocket create

Introduction

A UDP Multicast Socket That Receives UDP Multicast Messages

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class Main {
  public static void main(String[] args) {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    MulticastSocket mcSocket = null;
    InetAddress mcIPAddress = null;
    try {//from w  w  w  .  j  a  va 2s.  co m
      mcIPAddress = InetAddress.getByName(mcIPStr);
      mcSocket = new MulticastSocket(mcPort);
      System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress());

      // Join the group
      mcSocket.joinGroup(mcIPAddress);

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

      while (true) {
        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);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (mcSocket != null) {
        try {
          mcSocket.leaveGroup(mcIPAddress);
          mcSocket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}



PreviousNext

Related