[TCP/IP] Multicast - definition, principle and programming implementation (TTL, multicast group, multicast message)

Table of contents

multicast

The principle of multicast

Characteristics of multicast data transmission

The concept of TTL

How to configure TTL and multicast groups

Multicast programming and implementation

sender

receiver


multicast

        Multicast is a technical method between unicast and broadcast communication , which can send the data packets that the sender needs to send to a group of receivers scattered in different subnets.

The principle of multicast

        The basic concept of multicast is "group" . A multicast group is a group of receivers that wish to receive a specific data stream. This group has no physical or geographical boundaries: hosts in the group can be located anywhere on the Internet or a private network.

        Each node in a multicast group is called a multicast group member. The multicast data transmission protocol is based on UDP. When using multicast, data can be delivered to multiple hosts at the same time.

Characteristics of multicast data transmission

  • The multicast server only sends data once for a specific multicast group.
  • Even if the data is only sent once, all clients in the group will receive the data.
  • The number of multicast groups can be increased arbitrarily within the IP address range.
  • Join a specific group to receive data sent to that multicast group. 

        The address of the multicast group belongs to class D, that is, 224.0.0.0 ~ 239.255.255.255 . When sending multicast data packets (the router must support this function), a machine needs to join the multicast group to receive the incoming data packets, and the router is responsible for copying and delivering the data packets to multiple hosts. Task. As shown below:

The concept of TTL

        When transmitting multicast data packets, you need to set TTL (TIme to Live) for the program. This is a key parameter that determines whether the data packet can reach the destination point in time and accurately.

        TTL is represented by an integer, and the value is decremented by 1 every time it passes through a router. When the TTL becomes 0, the data packet can no longer be delivered and is destroyed. Therefore, the TTL value needs to be set appropriately. If it is too high, it will affect network traffic. If it is too small, the data packet will not be delivered to the target in time.

How to configure TTL and multicast groups

        In programming, we can set TTL. The protocol layer related to TTL is IPPROTO_IP, and the option name is IP_MULTICAST_TTL, which is set by using the setsocketopt function.

        code show as below:

int sock;
int ttl = 64;

sock = socket(PF_INET, SOCK_DGRAM, 0);
setsockopt(send_sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));

        Similarly, joining a multicast group is also done through the setsocketopt function. The protocol layer to join the multicast group is IPPROTO_IP, and the option name is IP_ADD_MEMBERSHIP. 

        code show as below:

int recv_sock;
struct ip_mreq groupjoin_adr;

recv_sock = socket(PF_INET, SOCK_DGRAM, 0);

join_adr.imr_multiaddr.s_addr = "多播组的地址";
join_adr.imr_interace.s_addr = "加入多播组的主机地址";
setsockopt(recv_sock , IPPROTO_IP , IP_ADD_MEMBERSHIP , (void*) & groupjoin_adr), sizeof(groupjoin_adr));

        The ip_mreq structure is defined as follows:

struct ip_mreq
  {
    /* 欲加入的多播组的地址.  */
    struct in_addr imr_multiaddr;

    /* 所属主机的IP地址. 可以使用INADDR_ANY  */
    struct in_addr imr_interface;
  };

Multicast programming and implementation

Distinguish between sender and receiver         in multicast . The sender refers to the subject sending multicast data, and the receiver refers to the subject located in the multicast group who wants to receive multicast data.

sender

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define TTL 64
#define BUF_SIZE 1024

void Sender_message(char *message)
{
	puts(message);
	exit(1);
}

int main(int argc, char *argv[])
{
	int send_sock;
	struct sockaddr_in mul_addr;
	int time2live = TTL;
	FILE *fp;
	char buf[BUF_SIZE];

	send_sock = socket(PF_INET, SOCK_DGRAM, 0);
	memset(&mul_addr, 0, sizeof(mul_addr));

	mul_addr.sin_family = AF_INET;
	mul_addr.sin_addr.s_addr = inet_addr(argv[1]); // 多播地址
	mul_addr.sin_port = htons(atoi(argv[2]));	   // 多播端口号

	setsockopt(send_sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&time2live, sizeof(time2live));

	fp = fopen(argv[3], "r");
	if (fp == NULL)
	{
		Sender_message((char*)"file open error");
	}

	while (!feof(fp)) /* 发送数据 */
	{
		fgets(buf, BUF_SIZE, fp);
		sendto(send_sock, buf, strlen(buf), 0, (struct sockaddr *)&mul_addr, sizeof(mul_addr));
	}

	fclose(fp);
	close(send_sock);
	return 0;
}

receiver

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 1024

//报错消息发送
void Sender_message(char *message)
{
    puts(message);
    exit(1);
}

int main(int argc, char *argv[])
{
    int recv_sock;
    int str_len;
    char buf[BUF_SIZE];
    struct sockaddr_in addr;
    struct ip_mreq join_addr;

    recv_sock = socket(PF_INET, SOCK_DGRAM, 0);
    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(atoi(argv[2]));

    if (bind(recv_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        Sender_message((char*)"bind error");
    }

    join_addr.imr_multiaddr.s_addr = inet_addr(argv[1]);
    join_addr.imr_interface.s_addr = htonl(INADDR_ANY);

    setsockopt(recv_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&join_addr, sizeof(join_addr));

    while (1)
    {
        // 接收多播数据 其中第五、第六个参数在不知道主机地址时可设为NULL和0
        str_len = recvfrom(recv_sock, buf, BUF_SIZE - 1, 0, NULL, 0);
        if (str_len < 0)
        {
            break;
        }
        buf[str_len] = 0;
        fputs(buf, stdout);
    }

    close(recv_sock);
    return 0;
}

operation result

Guess you like

Origin blog.csdn.net/weixin_42839065/article/details/131363281