多播的测试代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 10086
#define SIZE 128
int main(void)
{
int ret = -1;
int sockfd = -1;
int i = 0;
char buf[SIZE];
struct sockaddr_in addr;
struct sockaddr_in from;
//组播相关结构体
struct ip_mreq req;
socklen_t len = sizeof(from);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
perror("socket");
goto err0;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
//inet_pton(AF_INET, "172.16.1.88", &addr.sin_addr);
ret = bind(sockfd, (void*)&addr, sizeof(addr));
if (-1 == ret)
{
perror("bind");
goto err1;
}
printf("UDP Server %s: %d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
//加入多播组
req.imr_multiaddr.s_addr = inet_addr("224.0.0.88");
//将本机加入多播组
req.imr_interface.s_addr = INADDR_ANY;
//加入多播组
ret = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req));
if (ret < 0)
{
perror("setsockopt");
goto err0;
}
while(1)
{
memset(buf, 0, SIZE);
ret = recvfrom(sockfd, buf, SIZE, 0, (void*)&from, &len);
buf[ret] = 0;
printf("recv from: %s:%d %s\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port), buf);
i++;
//退出循环
if (10 == i)
break;
}
ret = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &req, sizeof(req));
if (ret < 0)
{
perror("setsockopt");
goto err0;
}
//退出组播组之后 测试是否可以收到组播包
while(1)
{
memset(buf, 0, SIZE);
ret = recvfrom(sockfd, buf, SIZE, 0, (void*)&from, &len);
buf[ret] = 0;
printf("recv from: %s:%d %s\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port), buf);
i++;
//退出循环
if (10 == i)
break;
}
close(sockfd);
return 0;
err1:
close(sockfd);
err0:
return -1;
}
在ubuntu编译运行时,出现如下的错误:
查询相关资料得到的答案如下:
It means that the tool is trying to use multicast but the network interface doesn't support it There are two likely causes:
·Your machine doesn't have multicast support enabled. For example, on Linux and FreeBSD it is possible to compile a kernel which doesn't support multicast.
·You don't have a route for multicast traffic. Some systems don't add this by default, and you need to run:
route add -net 224.0.0.0 netmask 255.255.255.255 eth0(or similar). If you wish to use RAT in unicast mode only, it is possible to add the multicast route on the loopback interface.
这主要和当前的网络配置有关,因为多播IP地址没有加入到路由表中。
解决方法:把需要用到的多播地址(如本例的224.0.0.88)加入到路由表中,命令如下:
sudo route add -net 224.0.0.88 netmask 255.255.255.255 eth0
224.0.0.88:为当前使用的多播IP地址
eth0:为当前使用的有效网卡
其它辅助命令:
sudo route del -net 224.0.0.88 netmask 255.255.255.255 eth0 //把224.0.0.88从路由表中删除
route -n //查看路由表信息
具体操作过程如下图: