boost :: asio :: ip :: multicast :: join_group不工作 [英] boost::asio::ip::multicast::join_group does not work

查看:1179
本文介绍了boost :: asio :: ip :: multicast :: join_group不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过示例,但它不工作。显然它不设置IPPROTO_IP / IP_MULTICAST_IF选项。我只能找到boost :: asio :: ip :: multicast :: outbound_interface为IPPROTO_IP / IP_MULTICAST_IF,我试过,但失败。有没有什么办法使boost :: asio :: ip ::多播工作,而不调用c级setsockopt?

I tried the example, but it does not work. Apparently it does not set IPPROTO_IP/IP_MULTICAST_IF option. I can only find boost::asio::ip::multicast::outbound_interface for IPPROTO_IP/IP_MULTICAST_IF, I tried but failed. Is there any way to make boost::asio::ip::multicast work without calling c-level setsockopt?

boost::asio::ip::udp::endpoint listen_endpoint(
    listen_address, multicast_port);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);

// Join the multicast group.
socket_.set_option(
    boost::asio::ip::multicast::join_group(multicast_address));


推荐答案

我认为 udp multicast 的示例代码。

在示例中代码,它们将套接字绑定到本地接口,但对于udp组播,您必须绑定到udp组播组IP和端口。

In the example code they bind socket to local interface but for udp multicast you have to bind to the udp multicast group IP and port.

socket_.bind(listen_endpoint);

应为:

socket_.bind(
    boost::asio::ip::udp::endpoint( multicast_address, multicast_port ) );

请参阅 multicast howto


...对于要接收组播数据报的进程,内核
加入组并绑定这些数据报正在发送的端口
到。 UDP层使用目的地址和端口到
多路分解数据包,并决定哪些套接字将它们传递给...

... for a process to receive multicast datagrams it has to ask the kernel to join the group and bind the port those datagrams were being sent to. The UDP layer uses both the destination address and port to demultiplex the packets and decide which socket(s) deliver them to ...

...我们必须要求内核加入那些
多播组...

... it is necessary to advise the kernel which multicast groups we are interested in. That is, we have to ask the kernel to "join" those multicast groups ...

检查您是否在 netstat -g | grep< multicast_group_ip>

这是我相信不正确的boost示例代码:

this is I believe incorrect boost example code:

boost::asio::ip::udp::endpoint listen_endpoint(
    listen_address, multicast_port);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);

// Join the multicast group.
socket_.set_option(
    boost::asio::ip::multicast::join_group(multicast_address));

socket_.async_receive_from(
    boost::asio::buffer(data_, max_length), sender_endpoint_,
    boost::bind(&receiver::handle_receive_from, this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));

这篇关于boost :: asio :: ip :: multicast :: join_group不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆