Multicast is a network technology that allows source processes to send packets to multiple target processes. A multicast source sends a packet to a specific multicast group, and only processes belonging to that multicast group can receive the packet. These processes can be in the same physical network or from different physical networks (as long as there is a multicast router support).
Multicast is divided into connectionless and connected-oriented multicast, but the basic multicast mechanism is connectionless, and what we are talking about here is also connectionless multicast.
We have said that we use the MulticastSocket class, which is called the Multicast Datagram Socket class, which is mainly used to send and receive IP multicast messages. MulticastSocket is a subclass of DatagramSocket, which adds the ability to join and leave multicast groups.A multicast group is defined by a combination of a Class D IP address and a standard UDP port number. The range of Class D IP addresses is 224.0.0.0~239.255.255.255.255. Except that 224.0.0.0 is a reserved address, it should not be used.
Below we use a simple example to demonstrate how two processes use multicast to communicate. One is the sending process and the other is the receiving process.
Let's look at the code:
import ; import ; import ; public class Sender { public static void main(String[] args) { try { byte[] msg = new byte[] { 'h', 'e', 'l', 'l', 'o' }; InetAddress inetAddress = ("230.0.0.1");//Return the host's IP address according to the host name DatagramPacket datagramPacket = new DatagramPacket(msg, , inetAddress, 7777);//The packet contains message content, message length, multicast IP and port MulticastSocket multicastSocket = new MulticastSocket(); (datagramPacket);//Send data packet } catch (Exception exception) { (); } } }
import ; import ; import ; public class Receiver { public static void main(String[] arstring) { try { MulticastSocket multicastSocket = new MulticastSocket(7777);//Create a multicast socket and bind to the sending port InetAddress inetAddress = ("230.0.0.1"); (inetAddress);//Add to the multicast socket to join the multicast group while (true) { byte[] data = new byte[100]; DatagramPacket datagramPacket = new DatagramPacket(data,);//Create a data packet for receiving data (datagramPacket);//Receive data packet (new String(data)); } } catch (Exception exception) { (); } } }
The key points of using MulticastSocket to implement multicast are as follows:
Receiver: Join the multicast group;
Sender: Send a datagram containing the group address.
By the way, let’s mention the practice of implementing private messages by multicast: we can add the specified receiver address to the message header, and then send it using multicast method. The key is to check the address when receiving it. If it matches the address, it will be received and processed; if it doesn’t match, of course it will be discarded. Of course, there is obviously a security problem. I will find a solution or a unicast mechanism in the future and write another blog post.
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.