Original address: http://blog.sina.com.cn/s/blog_49843aeb0100i0bc.html
Multicast
function: Inter-host communication usage, inter-routing communication usage
concept :IP multicast technology, which is one or more hosts (multicast source) that allows sending a single packet to multiple hosts (at the same time) TCP/IP network technology, is a point-to-multipoint communication
Why use it ?: In the application of network Multimedia broadcasting, when the signal of a node is transmitted to multiple nodes, whether it is using the repeated point-to-point communication or broadcasting, it will seriously waste network bandwidth, and only multicast is the best choice. Multicast enables one or more multicast sources to send data packets only to a specific multicast group, and only the host that joins the multicast group can receive the packet.
IP Multicast address :P Multicast communication relies on IP multicast address, in IPV4 it is a class D IP address, ranging from 224.0.0.0 to 239.255.255.255, and is divided into three categories: local link multicast address, reserved multicast address and management rights multicast address. where the local link multicast address range is in 224.0.0.0~224.0.0.255, which is reserved for routing protocols and other uses, the router does not forward IP packets belonging to this range, the reserved multicast address is 224.0.1.0~ 238.255.255.255, which can be used on a global scale (such as the Internet) or network protocols; The administrative rights multicast address is 239.0.0.0~239.255.255.255, which is used internally by the organization, similar to a private IP address, not to the Internet, The multicast range can be restricted.
Multicast group :All hosts that receive multicast packets using the same IP multicast address constitute a host group, also known as a multicast set. The membership of a multicast group is changed at any time, a host can join or leave the multicast group at any time, the number of multicast group members and the geographical location is not limited, a host can also belong to several multicast groups. Additionally, hosts that are not part of a multicast group can also send packets to the multicast group.
Java Example:
Server side:
Public classServer { Public StaticString group_ip = "239.1.1.1"; Public Static intListen_port = 22345; Public Static voidRun ()throwsunknownhostexception, IOException {System.out.println ("Server start ..."); MulticastSocket MulticastSocket=NewMulticastSocket (Listen_port); InetAddress Group=Inetaddress.getbyname (GROUP_IP); Multicastsocket.joingroup (group); byte[] data =New byte[50];//Unfilled space is filled by 0, and data exceeding the data length exceeds the array is ignoredDatagrampacket packet =Newdatagrampacket (data, data.length); while(true) { Try{multicastsocket.receive (packet); System.out.println (NewString (data) +packet.getaddress () + "" +NewSimpleDateFormat ("Yy-mm-dd-hh:mm:ss"). Format (NewDate ())); } Catch(IOException ex) {Joptionpane.showmessagedialog (NULL, "Multicast Error"); System.exit (1); } } } Public Static voidMain (string[] args) {Try{server.run (); }Catch(Exception e) {e.printstacktrace (); } } }
Client side:
Public classClient { Public Static voidRun ()throwsIOException, interruptedexception {System.out.println ("Client Start ..."); String name= "Clientid:5"; MulticastSocket MulticastSocket=NewMulticastSocket ();//Actually, this is the right place to send packet with Datagramsocket.InetAddress Group =Inetaddress.getbyname (SERVER.GROUP_IP); String msg= "Data become" +name; byte[] data =msg.getbytes (); Datagrampacket Packet=Newdatagrampacket (data, Data.length, Group, server.listen_port); intCount =1; while(true) { Try{multicastsocket.send (packet); System.out.println ("Send OK [" +name+ "]-->" +count); Count++; Thread.Sleep (10000); } Catch(IOException ex) {Joptionpane.showmessagedialog (NULL, "Multicast Error"); System.exit (1); } } } Public Static voidMain (string[] args) {Try{client.run (); }Catch(Exception e) {e.printstacktrace (); } }}
Server side can be added at any time or can quit at any time! Client-side messages can be sent to all servers that are joined!
Multicast Java programming