Linux-based multicast programming

Source: Internet
Author: User

Linux-based multicast programming

I. unicast, broadcast, and Multicast

1. unicast is used for end-to-end communication between two hosts, and broadcast is used for data communication between a host and all hosts on the LAN. Unicast and broadcast are two extremes: communication between a host or a host on the LAN. In practice, it is often necessary to communicate with a specific set of hosts, rather than all hosts on the LAN. This is the purpose of multicast.

2. Concept of Multicast:

1> multicast, also known as "Multicast", logically groups hosts of the same business type in the LAN. When sending and receiving data, the data is only grouped in the same group, other hosts are not added to this group and cannot send and receive corresponding data.

2> During wide area network broadcast, the switches and routers point to the host that needs to obtain the data to copy and forward the data. A host can send a request to a vro to join or exit a group. Routers and vswitches in the network can selectively copy and transmit data to only the hosts in the group.

3. Advantages and Disadvantages of Multicast

1> advantages:

The host with the same business type joins the same data stream, shares the same channel, saves the bandwidth and server advantages, and has the advantage of broadcasting without the bandwidth required for broadcasting.

The total bandwidth of the server is not limited by the client bandwidth. Because the multicast protocol depends on the receiver's needs to determine whether to forward data streams, the bandwidth on the server side is a constant and has nothing to do with the number of clients.

Same as unicast, multicast can be transmitted over the wide area network (WAN) or the Internet, whereas broadcast can only be transmitted over the same LAN.

2> disadvantages of Multicast:

Ø compared with unicast, multicast does not have an error correction mechanism, which is hard to compensate when an error occurs. However, this function can be implemented at the application layer.

Ø multicast network support is defective and requires support from routers and network protocol stacks.

4. Currently, multicast applications mainly include online videos and online conferences.

5. multicast address:

1> multicast addresses are specific. Class D addresses are used for multicast. Class d ip addresses are multicast IP addresses, that is, IP addresses between 224.0.0.0 and 239.255.255.255. They are divided into local continuous multicast addresses, reserved multicast addresses, and management permission multicast addresses. Category 3:

Ø local multicast address: In 224.0.0.0 ~ 224.0.0.251. This is the address reserved for the routing protocol and other purposes. The Router does not forward IP packets in this range. (That is, multicast IP addresses in this range can only be used within the LAN)

Ø reserved multicast address: In 224.0.1.0 ~ Between 238.255.255.255, applicable to global or network protocols

Management permission multicast address: In 239.0.0.0 ~ It can be used within an organization between 239.0000255 and is similar to a private IP address. It cannot be used on the Internet and can be used to limit the multicast range.

6. IPv4 ing IPv4 multicast addresses to Ethernet addresses:

1> the IPv4 Class D address (from 224.0.0.0 to 239.255.255.255) is an IPv4 multicast address. The class D address contains 28 low-order multicast group IDs ), the entire 32-bit address is called the Group address ).

2> IPv4 address ing, the 24-bit ethernet address is always 01: 00: 5E, the next bit is always 0, the 23-bit base order is copied from the 23-bit base order of the multicast group ID. In this way, the ethernet address of the multicast address is formed.

Ii. multicast programming

1> Create a socket;

2> set multicast parameters, such as time-out TTL and local loop.

3> join Multicast Groups

4> send and receive data

5> exit from multicast group

2. the multicast program is designed using the setsockopt () function and the getsockopt () function. The multicast option is at the IP layer.

3. setsockopt () Option

1> ip_multicast_ttl: sets the TTL value of multicast group data (route hops). Each time a router is crossed, the TTL value is reduced by one. The range is 0 ~ Any value between 255.

Int TTL;

Setsockopt (sock_fd, ipproto_ip, ip_multicast_ttl, & TTL, sizeof (TTL ));

2> ip_multicast_loop: by default, when the local machine sends multicast data to a network interface, at the IP layer, the data will be sent back to the fair local loopback interface, the ip_multicast_loop option is used to control whether data is sent back to the local loopback interface.

Int loop;

Setsockopt (sock_fd, ipproto_ip, ip_multicast_loop, & Loop, sizeof (loop ));

The loop parameter is set to 0, which indicates that delivery is not allowed. The loop parameter is set to 1 to allow delivery.

3> ip_add_membership: This option is added to a multicast group by controlling a variable of the struct ip_mreq type.

Struct ip_mreq

Struct ip_mreq

{

Struct in_addr imr_multiaddr;/* Multicast Group IP Address */

Struct in_addr imr_interface;/* network interface IP address added */

};

Ø the ip_add_membeship option is used to join a multicast group. Then, you can send data to or receive data from the multicast group. The value of this option is in the mreq structure. The member imr_multiaddr is the IP address of the multicast group to be added. The member imr_interface is the IP address of the network interface that the local machine needs to add to the multicast group.

Struct ip_mreq mreq;

Setsockopt (sock_fd, ipproto_ip, ip_add_membership, & mreq, sizeof (mreq ));

The ip_add_membership option can only be used to add IP addresses of one network interface to multicast groups at a time, but not one multicast group only allows one host IP address to join, you can call the ip_add_membership option multiple times to add multiple IP addresses to the same multicast group or add one IP address to Multiple Multicast Groups. When the imr_interface is inaddr_any, the local default network port is selected.

4> ip_drop_membership: This option is used to exit from a multicast group.

Struct ip_mreq mreq;

Setsockopt (sock_fd, ipprotp_ip, ip_drop_membership, & merq, sizeof (sreq ));

Example 3:

Scenario:

The server sends the data welcome you to multicast socket programme every 2 seconds to the destination port number 5000 and the destination multicast address 224.0.0.255.

The client receives 5 multicast data requests from the 224.0.0.255 multicast address and port number 5000.

1. server programs

1 # include <stdio. h>
2 # include <string. h>
3 # include <stdlib. h>
4 # include <unistd. h>
5 # include <sys/socket. h>
6 # include <sys/types. h>
7 # include <ARPA/inet. h>
8 # include <netinet/in. h>
9
10 # define serv_port 5350
11 # define mcast_port 5350
12 # define mcast_addr "224.0.0.133"
13 # define mcast_interval 2
14
15 int main (INT argc, char ** argv)
16 {
17 int sock_fd;
18 struct sockaddr_in mcast_addr, addr_serv, addr_client;
19 char send_buf [] = "tigerjibo ";
20 char recv_buf [1024];
21 int serv_len, mcast_len;
22 int send_num;
23 int recv_num;
24 int err;
25 struct ip_mreq mreq;
26 sock_fd = socket (af_inet, sock_dgram, 0);/* Create a socket */
27 if (sock_fd <0 ){
28 perror ("socket ()");
29 exit (1 );
30} else {
31 printf ("socket sucefull! \ N ");
32}
33/* server address */
34 memset (& addr_serv, 0, sizeof (struct sockaddr_in ));
35 addr_serv.sin_family = af_inet;
36 addr_serv.sin_addr.s_addr = inet_addr ("192.168.1.11 ");
37 addr_serv.sin_port = htons (serv_port );
38 serv_len = sizeof (struct sockaddr_in );
39 // initialize the multicast address
40 memset (& mcast_addr, 0, sizeof (mcast_addr ));
41 mcast_addr.sin_family = af_inet;
42 mcast_addr.sin_addr.s_addr = inet_addr (mcast_addr );
43 mcast_addr.sin_port = htons (mcast_port );
44
45 if (BIND (sock_fd, (struct sockaddr *) & addr_serv, serv_len) <0 ){

46 perror ("bind ");
47 exit (1 );
48} else {
49 printf ("bind sucess \ n ");
50}
51 while (1 ){
52 printf ("begin send: \ n ");
53 send_num = sendto (sock_fd, send_buf, sizeof (send_buf), 0, (struct sockaddr *) & mcast_addr, sizeof (mcast_addr ));
54 if (send_num <0 ){
55 perror ("sendto ()");
56 exit (1 );
57} else {
58 printf ("Send sucessful \ n ");
59}
60 sleep (mcast_interval );
61}
62 close (sock_fd );
63 return 0;
64}
64, 1 bottom

2. Client Program

1 # include <stdio. h>
2 # include <string. h>
3 # include <stdlib. h>
4 # include <errno. h>
5
6 # include <sys/socket. h>
7 # include <sys/types. h>
8 # include <ARPA/inet. h>
9 # include <netinet/in. h>
10 # include <unistd. h>
11 # define local_port 5350
12 # define mcast_addr "224.0.0.133"
13 # define mcast_interval 5
14 # define local_addr "192.168.1.11"
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 int main ()
34 {
35 int sock_fd;
36 struct sockaddr_in local_addr;
37 int err =-1;
38 int loop = 1;
39 sock_fd = socket (af_inet, sock_dgram, 0);/* Create a socket */
40 if (sock_fd <0 ){
41 perror ("socket ()");
42 exit (1 );
43} else {
44 printf ("socket () sucessful \ n ");

45}

46 memset (& local_addr, 0, sizeof (local_addr ));
47 local_addr.sin_family = af_inet;
48 local_addr.sin_addr.s_addr = htonl (inaddr_any); // inet_addr (local_addr); htonl (inaddr_any); local_addr cannot be accepted
49 local_addr.sin_port = htons (local_port );
50 err = BIND (sock_fd, (struct sockaddr *) & local_addr, sizeof (local_addr ));
51 if (ERR <0 ){
52 perror ("bind ");
53 exit (1 );
54} else {
55 printf ("bind sucessful \ n ");
56}
57 loop = 1;
58 err = setsockopt (sock_fd, ipproto_ip, ip_multicast_loop, & Loop, sizeof (loop ));
59 If (ERR <0 ){
60 perror ("setsocket (): IP multicast_loop ");
61 Exit (1 );
62} else {
63 printf ("ip_multicast_loop sucessful \ n ");
64}
65
66 struct ip_mreq mreq;
67 mreq. imr_multiaddr.s_addr = inet_addr (mcast_addr );
68 mreq. imr_interface.s_addr = inet_addr (local_addr );
69 err = setsockopt (sock_fd, ipproto_ip, ip_add_membership, & mreq, sizeof (mreq ));
70 If (ERR <0 ){
71 printf ("% s setsockopt (): IP add memburship \ n", strerror (errno ));
72 exit (1 );
73} else {
74 printf ("setsockopt () IP add memburship sucessful \ n ");
75}
76 int times = 0;
77 int addr_len = 0;
78 char buff [1000];
79 int recv_num = 0;
80 for (Times = 0; Times <5; times ++ ){
81 addr_len = sizeof (local_addr );
82 memset (buff, 0, sizeof (buff ));
83 recv_num = recvfrom (sock_fd, buff, sizeof (buff), 0, (struct sockaddr *) & local_addr, & addr_len );
84 If (recv_num <0 ){

85 perror ("recvfrom ()");
86 exit (1 );
87} else {
88 printf ("Recv % DST message from server: % s \ n", times, buff );
89 sleep (mcast_interval );
90}
91}
92 err = setsockopt (sock_fd, ipproto_ip, ip_drop_membership, & mreq, sizeof (mreq ));
93 close (sock_fd );
94 return 0;
95}

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.