c++ - 是否有可能获得多播组中每个套接字的所有 sockaddr_in 地址的列表

标签 c++ windows network-programming multicast

我正在创建一个小型的发送方和接收方应用程序,它将其名称广播到多播组中的所有其他应用程序,同时还接收其他应用程序的名称。

我想我现在已经完成了发送部分,如下所示:

if (sendto(sock, message, sizeof(message), 0, (struct sockaddr *) &receiver_addr, sizeof(receiver_addr)) < 0) {
            perror("sendto");
            exit(1);
        }

receiver_addr 的 ip 地址设置为多播组的 ip 地址,所以我确定组中的每个套接字都收到了消息。但是现在我想将从多播组中的每个套接字接收到的消息存储到一个数组中。我看到的示例代码使用以下代码:

if (recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *) &sender_addr, &senderlen) < 0)
        {
            perror("recvfrom");
            exit(1);
        }

这次 sender_addr 变量是数据来自的端口地址。但是这样只能得到地址为sender_addr的单个主机发送的信息。我想获得多播组内所有端口地址的列表,以便我可以运行 recvfrom 方法的循环,但将 receiver_addr 的值更改为组中每个套接字的地址。

最佳答案

Is it possible to get a list of all sockaddr_in addresses for each socket in a multicast group

没有。甚至不可能获得多播组成员的列表,更不用说它们的任何属性了。

the receiver_addr has its ip address set to the ip address of the multicast group, so I'm sure that each socket in the group has received the message.

不,你不是。多播运行在 UDP 之上,而 UDP 是一种不可靠的协议(protocol)。您无法确定任何此类事情。

I want to get a list of all addresses of ports inside the multicast group

运气不好。你不能。

so that I can run a loop of the recvfrom method, but change the value of receiver_addr to the address of each of the sockets in the group.

您对recvfrom()address 参数有一个基本的误解。它不是您事先设置的过滤器。它是一个result 参数,告诉您数据报是从哪里收到的。

关于c++ - 是否有可能获得多播组中每个套接字的所有 sockaddr_in 地址的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33911874/

相关文章:

Java网络/套接字编程教程

c++ - odeint : complex and higher precision types

c - 在 C 中获得无限输入?

c - 如何使用 libevent 库在程序中搜索和删除事件?

ios - 如何将保存的文本文件从我的 iOS 应用程序获取到 PC?

c++ - mingw64 (win8.1) 如何让他看到boost libs?

c - 如果我将服务器套接字绑定(bind)到设备,从 accept 生成的套接字描述符是否也会绑定(bind)到同一设备?

c++ - 默认模板参数中的 MPL 占位符替换

c++ - 使用 typedef 时,Eclipse CDT 可以自动完成吗?

c++ - CMake:如何添加具有相对目录的 Boost.Test 用例?