linux下组播的实现

组播及组播地址介绍

目前有三种通信方式:单播(unicast)、广播(broadcast)、组播(multicast)

单播解决了点对点通信的需求;

广播是点对多点的通信,其存在两个缺点:

1)只能在同一网段内实现广播;

2)不能指定目的主机,所有网段内的主机都将收到广播报文,存在带宽浪费。

组播也是点对多点的通信,完全克服了广播的两个缺点,广泛应用在网络电视、在线直播、远程教育、视频会议等对带宽和数据交互实时性较高的信息服务。

组播组可以是永久的也可以是临时的。组播组地址中,有一部分由官方分配的,称为永久组播组。永久组播组保持不变的是它的ip地址,组中的成员构成可以发生变化。永久组播组中成员的数量都可以是任意的,甚至可以为零。那些没有保留下来供永久组播组使用的ip组播地址,可以被临时组播组利用。
224.0.0.0~224.0.0.255 为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其它地址供路由协议使用;
224.0.1.0~224.0.1.255 是公用组播地址,可以用于Internet;欲使用需申请。
224.0.2.0~238.255.255.255 为用户可用的组播地址(临时组地址),全网范围内有效;临时的
239.0.0.0~239.255.255.255 为本地管理组播地址,仅在特定的本地范围内有效。

ip ad 命令查看网卡编号(程序有用到)

hann@ubuntu:~/hann/test/multicast$ ip ad
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:d6:fc:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.52.128/24 brd 192.168.52.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fdf0:1124:445f::2e2/128 scope global
valid_lft forever preferred_lft forever
inet6 fdf0:1124:445f:0:184:5851:1ba7:4686/64 scope global deprecated noprefixroute dynamic
valid_lft 2802sec preferred_lft 0sec
inet6 fe80::3a9a:36ee:25cb:bd22/64 scope link
valid_lft forever preferred_lft forever

linux中对struct ip_mreqn 结构体的定义

struct ip_mreqn {
		struct in_addr	imr_multiaddr;			/* IP multicast address of group */
		struct in_addr	imr_address;			/* local IP address of interface */
		int 			imr_ifindex;			/* Interface index */
};

server.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>

#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAXLINE 1500
#define GROUP "239.0.0.2"
//239.0.0.0~239.255.255.255
int main(void)
{
    int sockfd;
    struct sockaddr_in serveraddr, clientaddr;
    char buf[MAXLINE] = "multicast\n";
    struct ip_mreqn group;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);                /*构造用于UDP通信的套接字*/
    
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;                        /* IPv4 */
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);         /*本地任意IP INADDR_ANY = 0 */
    serveraddr.sin_port = htons(SERVER_PORT);

    bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

    inet_pton(AF_INET, GROUP, &group.imr_multiaddr);        /*设置组播组的地址*/
    inet_pton(AF_INET, "0.0.0.0", &group.imr_address);      /* 本地任意IP 自动分配有效IP*/
    group.imr_ifindex = if_nametoindex("eth0");             /* 给出网卡名,转换为对应编号:eth0 --> 编号         ,,  命令:ip ad */

    setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &group, sizeof(group));  /*获取组播权限*/

    bzero(&clientaddr, sizeof(clientaddr));                 /* 构造client 地址 IP+端口号*/
    clientaddr.sin_family = AF_INET;
    inet_pton(AF_INET, GROUP, &clientaddr.sin_addr.s_addr); /* IPv4  239.0.0.2+9000 */
    clientaddr.sin_port = htons(CLIENT_PORT);

    int i = 0;
    while (1) {
        sprintf(buf, "multicast %d\n", i++);
        //fgets(buf, sizeof(buf), stdin);
        sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
        sleep(1);
    }
    close(sockfd);
    return 0;
}

client.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>

#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define GROUP "239.0.0.2"

int main(int argc, char *argv[])
{
    struct sockaddr_in localaddr;
    int confd;
    ssize_t len;
    char buf[BUFSIZ];

    struct ip_mreqn group;                                                  /*组播结构体*/
    confd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&localaddr, sizeof(localaddr));                                   /* 初始化*/
    localaddr.sin_family = AF_INET;
    inet_pton(AF_INET, "0.0.0.0" , &localaddr.sin_addr.s_addr);
    localaddr.sin_port = htons(CLIENT_PORT);

    bind(confd, (struct sockaddr *)&localaddr, sizeof(localaddr));

    inet_pton(AF_INET, GROUP, &group.imr_multiaddr);                        /* 设置组播组地址*/
    inet_pton(AF_INET, "0.0.0.0", &group.imr_address);                      /*使用本地任意IP添加到组播组*/
    group.imr_ifindex = if_nametoindex("eth0");                             /* 设置网卡名 编号 ip ad */    
    setsockopt(confd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group));/* 将client加入组播组*/
    while (1) {
        len = recvfrom(confd, buf, sizeof(buf), 0, NULL, 0);
        write(STDOUT_FILENO, buf, len);
    }
    close(confd);
    return 0;
}

Makefile

src = $(wildcard *.c)
targets = $(patsubst %.c, %, $(src))

CC = arm-hisiv500-linux-gcc
CFLAGS = -Wall -g 

all:$(targets)

$(targets):%:%.c
	$(CC) $< -o $@ $(CFLAGS)

.PHONY:clean all
clean:
	-rm -rf $(targets) 

你可能感兴趣的:(UDP,网络编程)