UDP multicast Broadcast

Source: Internet
Author: User

Recently, I was writing a Distributed Instant chat program. To enable normal communication between Intranet users, I want to use multicast to broadcast data"

 

According to the query data, UDP broadcast can only be effective on the Intranet (the same network segment), while Multicast can better implement mass data across network segments.

 

 

  • Introduction


There are three transmission modes: unicast, multicast, and broadcast. In normal times, the most common one-to-one network transmission mode is unicast, while multicast is one-to-many transmission mode, where one multicast group
Concept: the sender sends data to a group, And the vro in the network automatically sends the data to all terminals listening to the Group through the IGMP protocol at the underlying layer. Broadcast is similar to multicast,
The difference is that the router delivers a packet to each terminal in the subnet, whether or not these terminals are willing to receive the packet.


Compared with broadcast that consumes extremely bandwidth (broadcast can only be broadcast on the Intranet), UDP multicast has been greatly optimized, and only terminals are added to a broadcast group, only UDP multicast data can be received by him.
UDP multicast adopts the connectionless and datagram connection mode, so it is unreliable. That is, the order in which the data can arrive at the receiver and the data arrival is not guaranteed. However, UDP does not need to guarantee data
The reliability of all data transmission speed is very fast.

  • IP multicast address

IP multicast communication requires a special multicast address, which is a group of D-class IP addresses ranging from 224.0.0.0
239.00000000255. Many other addresses are reserved for special purposes. 224.0.0.0 to 224.0.0.255 address is not recommended, because most of them are
For special purposes (for example, IGMP Protocol)

  • IGMP Protocol


IGMP is the basis of IP multicast. IGMP is generated to support multicast after the emergence of the IP protocol. What IGMP does is to tell the router that there is
A person is interested in the data sent to a multicast group, so that when the data of the multicast group arrives, the router will not discard it, instead, he is transferred to all interested customers. Assume that A and B in different subnets need
For multicast communication, all routers located between AB must support the IGMP protocol. Otherwise, AB cannot communicate with each other.

  • Basic Steps for UDP multicast
  1. Establish socket
  2. Socket and port binding
  3. Join a multicast group
  4. Send and receive data through sendto/recvfrom
  5. Disable socket
  • Send example

IPaddress IP = IPaddress. parse ("224.1.2.3 ");
Socket S = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
S. setsocketoption (socketoptionlevel. IP, socketoptionname. multicasttimetolive, 1 );
Ipendpoint ipep = new ipendpoint (ipendpoint, 5000 );
......
S. sendto (buff, Buff. length, socketflags. None, ipep );
......
S. Close ();

  • Recv example

Socket S = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
Ipendpoint ipep = new ipendpoint (ipaddresses. Any, 5000 );
S. BIND (ipep );
S. setsocketoption (socketoptionlevel. IP, socketoptionname. addmembership,
New multicastoption (IPaddress. parse ("224.1.2.3"), IPaddress. Any ));
......
S. Receive (B, 4, socketflags. None );
......
S. Close ();

  • Example: udpmulticast.7z

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.