UDP Multicast Broadcast

Recently I write a distributed real-time chat program, in order to achieve normal communication network users, consider using multicast way to achieve data "broadcast"

 

According to the information inquiry of view, UDP broadcast only the internal network (same network segment) effective, and can better achieve inter-network multicast bulk data.

 

 

  • Brief introduction

       IP network transmission is divided into a unicast, multicast (multicast), three kinds of broadcasting. Usually one of our most popular network belong unicast transmission; and-many multicast transmission mode in which the concept of a multicast group, the transmitting end transmits data to a group, network automatically send data through the router uses the IGMP to the bottom end all listeners of the group. As for the broadcast and multicast some similar, except that each router to a terminal in the delivery of a packet subnet, whether or not these terminals willing to receive the packet.

       With respect to the broadcast network bandwidth consumption is extremely (including only broadcast network broadcast), UDP multicast has been greatly optimized, only the terminal into a broadcasting group, UDP multicast data in order to be received by him. UDP multicast is used in connectionless, datagram connection, it is not reliable. I.e. the data can not reach the receiving terminal and the data arrival order is not guaranteed. However, because UDP do not guarantee the reliability of the data, all the data transmission speed is very fast.

  • IP Multicast Address

        IP Multicast communication requires a special multicast address, IP multicast address is a Class D IP addresses ranging from 224.0.0.0 to 239.255.255.255. Of which there are many addresses are reserved for special purposes. Address 224.0.0.0 to 224.0.0.255 is best not to use, because most of them are held for specific purposes (such as IGMP protocol)

  • IGMP protocol

        IGMP is the basis of IP multicast. After the IP protocol appears to add support for multicast, IGMP produced. IGMP does in fact tell the router, the router within the subnet where the data was sent to the interested one multicast group, so when the data arrives later this multicast group, the router will not abandon it, instead he forwarded to all interested customers. If A and B to be in a different subnet multicast communication, then all routers must be located between the AB support IGMP protocol, or can not communicate between AB.

  • The basic steps UDP multicast
  1. Establish socket
  2. socket and port binding
  3. Join a multicast group
  4. Transmits and receives data through sendto / recvfrom
  5. Close socket
  • Send examples

            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(ip, 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(IPAddress.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();

Reproduced in: https: //my.oschina.net/dake/blog/196723

Guess you like

Origin blog.csdn.net/weixin_34259159/article/details/91508852