c++ udp组播总结

概念

多播,也称为“组播”,将网络中同一业务类型主机进行了逻辑上的分组,进行数据收发的时候其数据仅仅在同一分组中进行,其他的主机没有加入此分组不能收发对应的数据。

特点

  • 优点
    • 具有同种业务的主机加入同一数据流,共享同一通道,节省了带宽;
    • 服务器的总带宽不受客户端带宽的限制,服务器端的带宽是常量,与客户端的数量无关;
    • 允许在广域网传输;
  • 缺点
    • 没有纠错机制;
    • 需要路由器和网络协议栈的支持;

流程

  • 发送端

    • 创建socket;
    // 第二个参数是 SOCK_DGRAM, 第三个参数0表示使用报式协议中的udp
    int socketFd = socket(AF_INET, SOCK_DGRAM, 0);
    if(-1 == socketFd)
    {
    	printf("create udp socket failed\n");
    	return -1;
    }
    
    • bind(可选)
    // 使用场景: 使用同一个socketFD接收消息(即recvfrom)
    // 说明: 一般将addr.sin_addr.s_addr设置为本机网卡地址
    struct sockaddr_in cliaddr;
    cliaddr.sin_family = AF_INET;
    cliaddr.sin_port = htons(18001);
    cliaddr.sin_addr.s_addr = INADDR_ANY;  // ???本地ip
    int ret = bind(socketFd, (struct sockaddr*)&cliaddr, sizeof(cliaddr));
    if(ret == -1)
    {
        printf("udp bind failed\n");
        return -1;
    }
    
    • 加入组播
    // 加入到组播
    struct ip_mreqn opt;
    opt.imr_address.s_addr = htonl(INADDR_ANY);
    inet_pton(AF_INET, "224.0.2.23", &opt.imr_multiaddr.s_addr);
    setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
    

    扩展:

    1)多播使用的是D类ip地址,即224.0.0.0至239.255.255.255之间的ip地址,并被划分为局部多播地址、预留多播地址和管理权限多播地址3类;

    1) 局部多播地址: 在224.0.0.0~224.0.0.255之间,这是为路由协议和其他用途保留的地址,路由器并不转发属于此范围的IP包。
    2) 预留多播地址: 在224.0.1.0~238.255.255.255之间,可用于全球范围(如Internet)或网络协议。
    3) 管理权限多播地址: 在239.0.0.0~239.255.255.255之间,可供组织内部使用,类似于私有IP地址,不能用于Internet,可限制多播范围。
    

    2)组播的选项值:

    **getsockopt()/setsockopt()**的选项 含义
    IP_MULTICAST_TTL 设置多播组数据的TTL值
    IP_ADD_MEMBERSHIP 在指定接口上加入组播组
    IP_DROP_MEMBERSHIP 退出多播组
    IP_MULTICAST_IF 获取默认接口或设置接口
    IP_MULTICAST_LOOP 禁止组播数据回送
    • 发送数据
    char buf[128] = {0,};
    snprintf(buf, sizeof(buf), "Get cpu state,i am %d", sessionKey);
    // 发送数据
    sendto(socketFd, buf, strlen(buf)+1, 0, (struct sockaddr*)&cliaddr, sizeof(cliaddr));
    
    • 接收数据(如果设置了bind可直接通过socketFd接收数据)
    char buf[128] = {0,};
    struct sockaddr_in sinAddr;
    socklen_t addrlen = sizeof(sinAddr);
    recvfrom(socketFd, buf, sizeof(buf), 0, (struct sockaddr*)&sinAddr, &addrlen);
    
  • 接收端

    • 创建socket
    int socketFd = socket(AF_INET, SOCK_DGRAM, 0);
    if(-1 == socketFd)
    {
        printf("create udp socket failed\n");
    	return -1;
    }
    
    • bind(必须)
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(18000);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    int ret = bind(fd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
    if(-1 == ret)
    {
        printf("udp bind failed\n");
        return -1;
    }
    
    • 加入组播
    struct ip_mreqn opt;
    opt.imr_address.s_addr = htonl(INADDR_ANY);
    inet_pton(AF_INET, "224.0.2.23", &opt.imr_multiaddr.s_addr);
    
    setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
    
    • 接收数据
    char buf[128] = {0,};
    struct sockaddr_in sinAddr;
    socklen_t addrlen = sizeof(sinAddr);
    recvfrom(socketFd, buf, sizeof(buf), 0, (struct sockaddr*)&sinAddr, &addrlen);
    
    • 发送数据
    char buf[128] = {0,};
    snprintf(buf, sizeof(buf), "Send cpu state,i am %d", sessionKey);
    // 发送数据
    sendto(socketFd, buf, strlen(buf)+1, 0, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
    

示例

  • 接收端
#include 
#include 
#include 
#include 
#include 
#include 

#define TIMEOUT 0xFFFFFFFF 

int main()
{
	// 1. create socket
	int socketFd = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == socketFd)
	{
    		printf("create udp socket failed\n");
		return -1;
	}

	// 2. bind
	struct sockaddr_in serveraddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(18000);
	serveraddr.sin_addr.s_addr = INADDR_ANY;
	int ret = bind(socketFd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
	if(-1 == ret)
	{
    		printf("udp bind failed\n");
    		return -1;
	}

	// 3. set param
	// 3.1 ttl
	int ttl = 64;
	setsockopt(socketFd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl));
	
	// 3.2 timeout
	unsigned int timeout = 2000;
	struct timeval tm;
	if(TIMEOUT == timeout)
	{
		tm.tv_sec = 0x7FFFFFFF;
		tm.tv_usec = 0;
	}
	else
	{
		tm.tv_sec = (timeout&0x7FFFFFFF) / 1000;
		tm.tv_usec = (timeout&0x7FFFFFFF) % 1000 * 1000;
	}
	setsockopt(socketFd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tm, sizeof(tm));

	// 4. join multi 
	struct ip_mreqn opt;
	opt.imr_address.s_addr = INADDR_ANY; // 本地地址
	inet_pton(AF_INET, "224.0.2.23", &opt.imr_multiaddr.s_addr);
	setsockopt(socketFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));

	// 5. recvfrom and sendto
	while(1)
	{
		char recv_buf[128] = {0,};
		int recvlen = recvfrom(socketFd, recv_buf, sizeof(recv_buf), 0, NULL, NULL);
		if(recvlen < 0)
		{
			//printf("server not recv msg,wait...\n");
			continue;
		}

		printf("server recv msg:%s\n", recv_buf);
		sleep(4);
/*
		char send_buf[128] = {0,};
		snprintf(send_buf, sizeof(send_buf), "Send cpu state,i am %d", 1);
		sendto(socketFd, send_buf, sizeof(send_buf), 0, (struct sockaddr*)&serveraddr, sizeof(serveraddr));

		printf("server send msg:%s\n", send_buf);
*/
	}

	return 0;
}

接收端程序运行时打印:

[root@localhost debug.x64-linux-g8]# ./testUdpServer
server recv msg:Send cpu state,i am 1
server recv msg:Send cpu state,i am 1
server recv msg:Send cpu state,i am 1
server recv msg:Send cpu state,i am 1
server recv msg:Send cpu state,i am 1
server recv msg:Send cpu state,i am 1
  • 发送端
#include 
#include 
#include 
#include 
#include 
#include 

#define TIMEOUT 0xFFFFFFFF 

int main()
{
	// 1. create socket
	int socketFd = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == socketFd)
	{
    		printf("create udp socket failed\n");
		return -1;
	}

	// 2. bind
/*
	struct sockaddr_in clientaddr;
	clientaddr.sin_family = AF_INET;
	clientaddr.sin_port = htons(18001);
	clientaddr.sin_addr.s_addr = INADDR_ANY;
	int ret = bind(socketFd, (struct sockaddr*)&clientaddr, sizeof(clientaddr));
	if(-1 == ret)
	{
    		printf("udp bind failed\n");
    		return -1;
	}
*/

	// 3. set param
	// 3.1 ttl
	int ttl = 64;
	setsockopt(socketFd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl));
	
	// 3.2 timeout
	unsigned int timeout = 2000;
	struct timeval tm;
	if(TIMEOUT == timeout)
	{
		tm.tv_sec = 0x7FFFFFFF;
		tm.tv_usec = 0;
	}
	else
	{
		tm.tv_sec = (timeout&0x7FFFFFFF) / 1000;
		tm.tv_usec = (timeout&0x7FFFFFFF) % 1000 * 1000;
	}
	setsockopt(socketFd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tm, sizeof(tm));

	// 4. set IP_MULTICAST_IF
	struct in_addr opt;
    inet_pton(AF_INET, "224.0.2.23", &opt.s_addr);
    setsockopt(socketFd, IPPROTO_IP, IP_MULTICAST_IF, &opt, sizeof(opt));

    struct sockaddr_in clientaddr;
    clientaddr.sin_family = AF_INET;
    clientaddr.sin_port = htons(18000); 
    inet_pton(AF_INET, "224.0.2.23", &clientaddr.sin_addr.s_addr);

	// 5. recvfrom and sendto
	while(1)
	{
		char send_buf[128] = {0,};
		snprintf(send_buf, sizeof(send_buf), "Send cpu state,i am %d", 1);
		sendto(socketFd, send_buf, sizeof(send_buf), 0, (struct sockaddr*)&clientaddr, sizeof(clientaddr));

		printf("client send msg:%s\n", send_buf);
		sleep(2);
/*
		char recv_buf[128] = {0,};
		struct sockaddr_in sinAddr;
		socklen_t addrlen = sizeof(sinAddr);
		int recvlen = recvfrom(socketFd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr*)&sinAddr, &addrlen);
		if(recvlen < 0)
		{
			printf("client not recv msg,wait...\n");
			continue;
		}

		printf("client recv msg:%s\n", recv_buf);
*/
	}

	return 0;
}

发送端程序运行时打印:

[root@localhost debug.x64-linux-g8]# ./testUdpClient
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1
client send msg:Send cpu state,i am 1

你可能感兴趣的:(c++,c++,udp组播)