UDP Multicast---you need to know that.

Source: Internet
Author: User
Tags reserved
first to understand the next UDP

UDP is the abbreviation of Userdatagram protocol, the Chinese name is User Datagram Protocol, is a connectionless transport layer protocol in the Reference Model of OSI (open System interconnection, open Systems Interconnection). Provides a transaction-oriented, simple, unreliable information delivery service, and IETF RFC 768 is the formal specification of UDP. The protocol number for UDP in IP packets is 17.

The difference between UDP and TCP: http://blog.csdn.net/ljheee/article/details/50823050

UDP Detailed: http://blog.csdn.net/ljheee/article/details/51720594


the way of UDP information transmission is divided into three kinds

① Unicast unicast: A point-to-point connection between the client and the server.

② Broadcast broadcast: "A pair of all" communication modes between hosts, broadcasters can send information to all hosts in the network. Broadcasts prohibit transmission on Internet broadband Internet (broadcast storm).

③ Multicast Multicast: A "pair of" communication mode between hosts, in which a host that joins the same group can accept all data in this group.

It should be noted here that only UDP has broadcast, multicast delivery mode, and TCP is a one-to-one connection communication. The focus of multicasting is to efficiently send the same package as much as possible to different, or possibly unknown, devices. But the TCP connection is a pair of clear, only unicast.

In the Java API, the implementation of UDP programming, including client network programming and server-side network programming, is mainly implemented by two classes, respectively: Datagramsocket and Datagrampacket.

A detailed example of UDP usage: Https://github.com/ljheee/ChatUDP

UDP Multicast

The purpose of the group broadcast is to use Class D IP addresses, and Class D addresses cannot appear in the Source IP address field of IP packets. In the process of unicast data transmission, a packet transfer path is routed from the source address to the destination address, and the "hop-by" principle [routing] is used to transmit in the IP network.

However, in the IP multicast ring, the destination address of the packet is not one, but a group, which forms the group address. All information receivers are added to a group, and once added, data flowing to the group address begins to transfer to the receiver immediately, and all members of the group receive the packet. The members of the multicast group are dynamic and the host can join and leave the multicast group at any time.

All hosts that receive multicast packets with the same IP multicast address form a host group, also known as a multicast set. The members of a multicast group are subject to change, a host can join or leave the multicast group at any time, the number of members of the multicast group and the geographic location are unrestricted, and a host can belong to several multicast groups.   In addition, a host that is not part of a multicast group can send packets to that multicast group. Multicast address Multicast groups can be permanent or temporary. A part of the multicast group address is officially assigned, called a permanent multicast group. The permanent multicast group remains unchanged by its IP address, and the membership of the group can vary. The number of members in a permanent multicast group can be arbitrary or even zero. IP multicast addresses that are not retained for use by permanent multicast groups can be exploited by temporary multicast groups. 224.0.0.0~224.0.0.255 is reserved for the multicast address (Permanent group address), the address 224.0.0.0 reserved, other addresses for routing protocol use, 224.0.1.0~224.0.1.255 is a public multicast address, can be used for the Internet; 224.0.2.0~238.255.255.255 for user-available multicast addresses (temporary group addresses), full network-wide validity, 239.0.0.0~239.255.255.255 for local administration multicast addresses, valid only within a specific local scope.

Multicast is a one-to-many transmission, which has the concept of a multicast group, sending the data to a group sent, the network routers through the underlying IGMP protocol to automatically send data to all the terminals listening to this group. As for broadcasting, it is similar to multicast, except that routers deliver a packet to each terminal in the subnet, regardless of whether they are happy to receive the packet. UDP broadcasts can only be effective in intranet (the same network segment), and multicast is a good way to achieve mass data across the network segment.

UDP multicast is the use of connectionless, datagram connection, so it is unreliable. That is, the data can not reach the receiving end and the order of data arrival is not guaranteed. However, because UDP does not guarantee the reliability of data, the transmission efficiency of all data is very fast.


IGMP Protocol

IGMP is the basis of IP multicast. After the advent of the IP protocol, IGMP was created to add support for multicast. What IGMP does is actually tell the router that there is someone in the subnet of the router that is interested in the data sent to a multicast group, so that when the data for the multicast group arrives, the router will not abandon it, but instead transfer it to all interested customers. If A and B in different subnets are to be multicast communication, then all routers between AB must support the IGMP protocol, otherwise there is no communication between AB.

the principle of multicast: multicast is first applied by a user to a multicast group, the multicast group is maintained in the router, and other users apply to join the multicast group, so that when a user sends a message to the group, the router forwards the message to all members of the group. If the requesting group does not exist in this level of routing, if the router and switch allow the multicast protocol to pass, the router submits the requested join operation to the parent route. WAN communication to pass through multi-level routers and switches, almost all network devices are blocked by default multicast protocol (only allowed in the network segment, not to the superior), which makes the implementation of multicast on the WAN has certain limitations.

basic steps for UDP multicast establish socket socket and port bindings join a multicast group to send and receive data by Sendto/recvfrom close socket

Both the server and the client must be joined to the same multicast address.

Multicast datagram socket classes are used to send and receive IP multicast packets. MulticastSocket is a (UDP) Datagramsocket that has additional features for groups that join other multicast hosts on the Internet.

Multicast groups are specified through class D IP addresses and standard UDP port numbers. You can join a multicast group by first creating the MulticastSocket using the desired port and then calling the Joingroup (inetaddress groupaddr) method.

In Java, multicast is a very good implementation, to achieve multicast, it is necessary to use the MulticastSocket class, in fact, the class is Datagramsocket subclass, in addition to the use of a number of their own characteristics, as a datagramsocket class use it.

By default we know that IP (UDP and TCP) can send packets to another device in one network. The more accurate point is that IP sends packets from one IP address to another IP address. The trick of multicast is to send a packet to multiple devices at the same time, specifying a specific IP address as a multicast address and sending it to multiple devices simultaneously.

IP multicast should first know that only UDP has multicast, there is no TCP multicast such things, why. The focus of multicasting is to efficiently send the same package as much as possible to different, or possibly unknown, devices. However, TCP connections may require a packet loss or delay or reorganization order, which can be very resource-intensive and not suitable for many scenarios where multicast is used. (At the same time multicast does not know whether the sent packets have arrived, this also leads to the inability to use TCP).

Finally, an example is given: Https://github.com/ljheee/Multicast_UDP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.