1.概念
单播,是用于两个主机之间传送数据;
广播,是一个主机对局域网内的所有主机发送数据;
多播,又称为组播,它是对一组特定的主机通信。
将网络上同一类型业务逻辑上分组,只和组内的成员通信,其它主机没有加入组则不能通信。与单播相同的是,组播允许在Internet上通信,而广播只是同一局域网内的主机通信。组播地址是特定的,D类地址用于组播,即244.0.0.0到239.255.255.255. 并划分为局部连接多播地址,预留多播地址和管理权限多播地址3类。
(1)局部多播地址 (224.0.0.-224.0.0.255)为路由协议和其它用途保留的地址,路由器不转发此范围的IP包
(2)预留多播地址 (224.0.1.0-238.255.255.255)可用于全球范围内或网络协议
(3)管理权限的多播 (239.0.0.0-239.255.255.255) 可供组织内使用,类型于私有IP,不用于Internet,可限制多播范围
2. 多播套接字设置
可用setsockopt或getsockopt设置或得到多播选项. 常用的多播选项如下所示:
IP_MULTICAST_TTL 设置多播的TTL值
Sets the Time To Live (TTL) in the IP header for outgoing multicast datagrams. By default it is set to 1. TTL of 0 are not transmitted on any sub-network. Multicast datagrams with a TTL of greater than 1 may be delivered to more than one sub-network, if there are one or more multicast routers attached to the first sub-network.
IP_MULTICAST_IF 获取或设置多播接口
Sets the interface over which outgoing multicast datagrams are sent.
IP_MULTICAST_LOOP 禁止多播数据回送到本地loop接口
Specifies whether or not a copy of an outgoing multicast datagram is delivered to the sending host as long as it is a member of the multicast group.
IP_ADD_MEMBERSHIP 将指定的接口加入多播
Joins the multicast group specified.
IP_DROP_MEMBERSHIP 退出多播组
Leaves the multicast group specified.
struct ip_mreq{
struct in_addr imn_multicastaddr;//多播组地址
struct in_addr imr_interface;//加入的接口的IP地址
}
int ttl=255;
setsockopt(s,IPPROTO_IP,IP_MULTICAST_TTL,&ttl,sizeof(ttl));//设置跳数
s-套接字描述符
PROTO_IP-选项所在的协议层
IP_MULTICAST_TTL-选项名
&ttl-设置的内存缓冲区
sizeof(ttl)-设置的内存缓冲区长度
struct in_addr in;
setsockopt(s,IPPROTO_IP,IP_MUTLICAST_IF,&in,sizeof(in));//设置组播接口
int yes=1;
setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&yes,sizeof(yes));//设置数据回送到本地回环接口
struct ip_mreq addreq;
setsockopt(s,IPPROTO_IP,IP_ADD_MEMBERSHIP,&req,sizeof(req));//加入组播组
struct ip_mreq dropreq;
setsockopt(s,IPPROTO_IP,IP_DROP_MEMBERSHIP,&dropreq,sizeof(dropreq));//离开组播组
3. 多播程序的设计流程
(1)建立socket
(2)设置TTL值 IP_MULTICAST_TTL
(3)设置是否允许本地回环 IP_MULTICAST_LOOP
(4)加入多播组 IP_ADD_MEMBERSHIP
(5)发送数据 send
(6)接收数据 recv
(7)退出多播组 IP_DROP_MEMBERSHIP
mulcastserver.c
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#define BUFLEN 255 int main(int argc, char **argv)
{
struct sockaddr_in peeraddr;
struct in_addr ia;
int sockfd;
char recmsg[BUFLEN + ];
unsigned int socklen, n;
struct hostent *group;
struct ip_mreq mreq;
/* 创建 socket 用于UDP通讯 */
sockfd = socket(AF_INET, SOCK_DGRAM, );
if (sockfd < )
{
printf("socket creating err in udptalk\n");
exit();
}
/* 设置要加入组播的地址 */
bzero(&mreq, sizeof(struct ip_mreq));
if (argv[])
{
if ((group = gethostbyname(argv[])) == (struct hostent *) )
{
perror("gethostbyname");
exit(errno);
}
}
else
{
printf("you should give me a group address, 224.0.0.0-239.255.255.255\n");
exit(errno);
} bcopy((void *) group->h_addr, (void *) &ia, group->h_length);
/* 设置组地址 */
bcopy(&ia, &mreq.imr_multiaddr.s_addr, sizeof(struct in_addr));
/* 设置发送组播消息的源主机的地址信息 */
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
/* 把本机加入组播地址,即本机网卡作为组播成员,只有加入组才能收到组播消息 */
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,sizeof(struct ip_mreq)) == -)
{
perror("setsockopt");
exit(-);
} socklen = sizeof(struct sockaddr_in);
memset(&peeraddr, , socklen);
peeraddr.sin_family = AF_INET; if (argv[])
{
peeraddr.sin_port = htons(atoi(argv[]));
}
else
{
peeraddr.sin_port = htons();
} if (argv[])
{
if (inet_pton(AF_INET, argv[], &peeraddr.sin_addr) <= )
{
printf("Wrong dest IP address!\n");
exit();
}
}
else
{
printf("no group address given, 224.0.0.0-239.255.255.255\n");
exit(errno);
}
/* 绑定自己的端口和IP信息到socket上 */
if (bind(sockfd, (struct sockaddr *) &peeraddr,sizeof(struct sockaddr_in)) == -)
{
printf("Bind error\n");
exit();
}
/* 循环接收网络上来的组播消息 */
for (;;)
{
printf("before recvfrom!\n");
bzero(recmsg, BUFLEN + );
n = recvfrom(sockfd, recmsg, BUFLEN, ,(struct sockaddr *) &peeraddr, &socklen);
if (n < )
{
printf("recvfrom err in udptalk!\n");
exit();
}
else
{
/* 成功接收到数据报 */
recmsg[n] = ;
printf("server peer:%s\n", recmsg);
}
}
}
mulcastclient.c
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 255 int main(int argc, char **argv)
{
struct sockaddr_in peeraddr, myaddr;
int sockfd;
char recmsg[BUFLEN + ];
unsigned int socklen;
/* 创建 socket 用于UDP通讯 */
sockfd = socket(AF_INET, SOCK_DGRAM, );
if (sockfd < )
{
printf("socket creating error\n");
exit();
}
socklen = sizeof(struct sockaddr_in);
/* 设置对方的端口和IP信息 */
memset(&peeraddr, , socklen);
peeraddr.sin_family = AF_INET;
if (argv[])
{
peeraddr.sin_port = htons(atoi(argv[]));
}
else
{
peeraddr.sin_port = htons();
} if (argv[])
{
/* 注意这里设置的对方地址是指组播地址,而不是对方的实际IP地址 */
if (inet_pton(AF_INET, argv[], &peeraddr.sin_addr) <= )
{
printf("wrong group address!\n");
exit();
}
}
else
{
printf("no group address!\n");
exit();
} /* 设置自己的端口和IP信息 */
memset(&myaddr, , socklen);
myaddr.sin_family = AF_INET;
if (argv[])
{
myaddr.sin_port = htons(atoi(argv[]));
}
else
{
myaddr.sin_port = htons();
} if (argv[])
{
if (inet_pton(AF_INET, argv[], &myaddr.sin_addr) <= )
{
printf("self ip address error!\n");
exit();
}
}
else
{
myaddr.sin_addr.s_addr = INADDR_ANY;
} /* 绑定自己的端口和IP信息到socket上 */
if (bind(sockfd, (struct sockaddr *) &myaddr,sizeof(struct sockaddr_in)) == -)
{
printf("Bind error\n");
exit();
}
/* 循环接受用户输入的消息发送组播消息 */
for (;;)
{
/* 接受用户输入 */
bzero(recmsg, BUFLEN + );
if (fgets(recmsg, BUFLEN, stdin) == (char *) EOF)
{
exit();
}
/* 发送消息 */
if (sendto(sockfd, recmsg, strlen(recmsg), ,(struct sockaddr *) &peeraddr,sizeof(struct sockaddr_in)) < )
{
printf("sendto error!\n");
exit();
} printf("client:'%s'send ok\n", recmsg);
}
}