SoFunction
Updated on 2025-03-11

Android uses MulticastSocket to implement multicast pictures

DatagramSocket only allows datagrams to be sent to specified destination addresses, while MulticastSocket can send datagrams to multiple clients in a broadcast manner. The main idea is to set a set of special network addresses as multicast addresses. Each multicast address is regarded as a group. When the client needs to send and receive broadcast messages, it can be added to the group.

The IP protocol provides these special IP addresses for multicasting, with the range of these IP addresses ranging from 224.0.0.0 to 239.255.255.255.255. When the MulticastSocket sends a DatagramPacket to the multicast IP address, the data will be automatically broadcast to all MulticastSockets that join the address. At the same time, the MulticastSocket can also be set to receive the data it sends.

If it is just a MulticastSocket object used to send datagrams, use the default address and a random port. However, if you create a MulticastSocket object for receiving, the MulticastSocket object must specify a port, otherwise the sender will not be able to determine the destination port for sending the datagram.

The following is a simple example to implement multicast pictures:

Multicast tool category:

public class ComUtil
{
 public static final String BROADCAST_IP = "224.2.2.2";
 public static final int BOADCAST_PORT = 30000;
 private static final int DATA_LEN = 100 * 1024;
 //Define the MulticastSocket instance of this program private MulticastSocket socket = null;
 //Define the IP address of the broadcast private InetAddress broadcastAddress = null;
 //Define the character array that receives network data byte[] inBuff = new byte[DATA_LEN];
 //Create DatagramPacket object ready to be accepted with a specified byte array private DatagramPacket inPacket = new DatagramPacket(inBuff , );
 //Define a DatagramPacket object for sending private DatagramPacket outPacket = null;
 private Handler handler;

 //Constructor, initialize resource public ComUtil(Handler handler) throws Exception
 {
  = handler;
 //Because the MulticastSocket object needs to receive data, there is a specified port socket = new MulticastSocket(BOADCAST_PORT);
 broadcastAddress = (BROADCAST_IP);
 //Add the socket to the specified multicast address (broadcastAddress);
 //Set the datagram sent by this MultcastSocket will be sent to itself (false);
 //Initialize the DatagramSocket for sending, which contains an array of bytes of length 0 outPacket = new DatagramPacket(new byte[0] , 0 , broadcastAddress , BOADCAST_PORT);
 new ReadBroad().start();
 }

 //Tools and methods for broadcasting messages public void broadCast(byte[] msg)
 {
 try
 {
  //Convert msg string to byte array  byte[] buff = msg;
  //Set the byte array in the DatagramPacket for sending  (buff);
  //Send data  (outPacket);
 }
 catch (IOException e)
 {
  ();
 }
 }

 //Continuously read the thread of MulticastSocket class ReadBroad extends Thread
 {
 public void run()
 {
  while (true)
  {
  try
  {
   //Read the data in the Socket   (inPacket);
   Message msg = new Message();
    = 0x123;
    = inBuff;
   (msg);
  }
  catch (IOException e)
  {
   ();
  }
  }
 }
 }
}

MainActivity class:

public class MainActivity extends Activity
{
 private Button button;
 private ImageView img;
 private ComUtil comUitl;
 Handler handler = new Handler()
 {
 @Override
 public void handleMessage(Message msg)
 {
  if ( == 0x123)
  {
  byte[] result = (byte[]) ;
  ((result , 0 , ));
  }
 }
 };

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 (savedInstanceState);
 setContentView(.main_activity);
 try
 {
  comUitl = new ComUtil(handler);
 }
 catch (Exception e)
 {
  ();
 }

 button = (Button) findViewById(.send_img_all);
 img = (ImageView) findViewById(.receiver_img);
 (new ()
 {
  @Override
  public void onClick(View view)
  {
  sendData();
  }
 });
 }

 private void sendData()
 {
 Bitmap bitmap = (getResources() , .wenqing2);
 ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
 ( , 100 , byteArray);
 ();
 final byte[] msg = ();
 new Thread()
 {
  @Override
  public void run()
  {
  (msg);
  }
 }.start();

 try
 {
  ();
 }
 catch (IOException e)
 {
  ();
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.