【问题标题】:source-specific multicast using boost使用 boost 的源特定多播
【发布时间】:2011-12-26 15:27:34
【问题描述】:

如何使用 boost 加入特定于源的多播 (ssm) 组?

我已经使用

成功加入了任意源多播组

set_option( ip::multicast::join_group( mcAddr.to_v4(), ifAddr.to_v4( )));

但我不知道如何设置 ssm (IP_ADD_SOURCE_MEMBERSHIP)。

我该怎么做?谢谢。

【问题讨论】:

    标签: c++ boost udp boost-asio multicast


    【解决方案1】:

    是的。我用这个

    //---------------------------------------------------------------------------------------------------------
    // class add_source_membership_v4
    // is for Windows Sockets 2 IPv4
    // to set IP_ADD_SOURCE_MEMBERSHIP option.
    //---------------------------------------------------------------------------------------------------------
    class add_source_membership_v4
    {
    private:
        ip_mreq_source ipv4_value_{};
    
    public:
        // Default constructor.
        // Construct with multicast address and IPv4 address specifying an interface.
        add_source_membership_v4(
            const boost::asio::ip::address_v4& multicast_address,
            const boost::asio::ip::address_v4& source_address,
            const boost::asio::ip::address_v4& network_interface = boost::asio::ip::address_v4::any())
        {
            ipv4_value_.imr_multiaddr.s_addr =
                boost::asio::detail::socket_ops::host_to_network_long(
                    multicast_address.to_uint());
            ipv4_value_.imr_sourceaddr.s_addr =
                boost::asio::detail::socket_ops::host_to_network_long(
                    source_address.to_uint());
            ipv4_value_.imr_interface.s_addr =
                boost::asio::detail::socket_ops::host_to_network_long(
                    network_interface.to_uint());
        }
    
        // Returns a value suitable for passing as the level argument to POSIX setsockopt() (or equivalent).
        template <typename Protocol>
        int level(const Protocol&) const noexcept
        {
            return BOOST_ASIO_OS_DEF(IPPROTO_IP);
        }
    
        // Returns a value suitable for passing as the option_name argument to POSIX setsockopt() (or equivalent).
        template <typename Protocol>
        int name(const Protocol&) const noexcept
        {
            return IP_ADD_SOURCE_MEMBERSHIP;
        }
    
        // Returns a pointer suitable for passing as the option_value argument to POSIX setsockopt() (or equivalent).
        template <typename Protocol>
        const void* data(const Protocol&) const noexcept
        {
            return &ipv4_value_;
        }
    
        // Returns a value suitable for passing as the option_len argument to POSIX setsockopt() (or equivalent),
        // after appropriate integer conversion has been performed.
        template <typename Protocol>
        std::size_t size(const Protocol&) const noexcept
        {
            return sizeof(ipv4_value_);
        }
    };
    

    【讨论】:

      【解决方案2】:

      看起来你必须实现他们的SettableSocketOption 概念。检查现有的多播选项是如何完成的 (svn link) 可能会有所帮助。

      【讨论】:

      • 试图打开链接并得到“此服务器无法验证您是否有权访问请求的文档”错误
      猜你喜欢
      • 1970-01-01
      • 2015-06-18
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多