C # implements arbitrary source multicast with specific source multicast

Source: Internet
Author: User

IP multicast traffic requires a special multicast address, the IP multicast address is a group of class D IP addresses ranging from 224.0.0.0 to 239.255.255.255. Many of these addresses are reserved for special purposes. 224.0.0.0 to 224.0.0.255 's address is best not to use because they are mostly kept for special purposes (such as the IGMP protocol).

IGMP is the basis for IP multicast. IGMP generated after the IP protocol appeared in order to join support for multicast. What IGMP does is to tell the router that someone is interested in the data sent to a certain multicast group in the subnet where the router is located, so that when the multicast group's data arrives, the router will not abandon it, but transfer it to all interested clients. If A and B in different subnets are to be multicast, then all routers located between AB must support the IGMP protocol, otherwise the AB cannot communicate.

1 using sockets for any source multicastThe basic steps to implement UDP multicast using C # are:
(1) socket and (2) socket and port binding;
(3) to join a multicast group country;
(4) Sending and receiving data through Sendto/recvfrom;
(5) Close the socket. The following is a simple example: (1) Send example: IPAddress IP = ipaddress.parse ("226.1.1.2");
Socket s = new socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP);
S.setsocketoption (Socketoptionlevel.ip, socketoptionname.multicasttimetolive, 1);
IPEndPoint Ipep = new IPEndPoint (IP, 5000);
......
S.sendto (buff, buff. Length, Socketflags.none, IPEP);
......
S.close (); (2) Receive example: socket s = new socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP);
IPEndPoint Ipep = new IPEndPoint (Ipaddress.any, 5980);
S.bind (IPEP);
S.setsocketoption (Socketoptionlevel.ip, Socketoptionname.addmembership,
New MulticastOption (Ipaddress.parse ("226.1.1.2"), Ipaddress.any));
......
S.receive (b, 4, socketflags.none);
......
S.close (); 2 using a socket to implement a specific source multicast specific source multicast (source specific multicast, SSM) provides a "channel" abstraction to the host application, with one source and multiple receivers per channel. The multicast source can send an IP message to the SSM destination address G, and the recipient can join the channel (S,G) to receive the message. The IPV4 address range 232/8 has been assigned to the SSM service by the IANA. When implementing a specific source multicast using C #, the Socketoption parameter needs to be set as follows: byte[] membershipaddresses = new BYTE[12]; 3 IPs * 4 bytes (IPV4)
Buffer.blockcopy (Multicastip.getaddressbytes (), 0, membershipaddresses, 0, 4); Multicast address
Buffer.blockcopy (Sourceip.getaddressbytes (), 0, Membershipaddresses, 4, 4); Multicast Source Address
Buffer.blockcopy (Localip.getaddressbytes (), 0, Membershipaddresses, 8, 4); Local Receive Address
Socket. SetSocketOption (Socketoptionlevel.ip, Socketoptionname.addsourcemembership, membershipaddresses); http://m.blog.csdn.net/article/details?id=11715903

C # implements arbitrary source multicast with specific source multicast

Related Article

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.