Boost UDP多播接收器:set_option:请求的地址在其上下文中无效 [英] Boost UDP multicast receiver: set_option: The requested address is not valid in its context

查看:520
本文介绍了Boost UDP多播接收器:set_option:请求的地址在其上下文中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Boost的UDP时间服务器教程

I am following Boost's UDP Time server tutorial here . I modify the code with these for predefined values:

short multicast_port = 13;  // (in class receiver)

和main()中的

//if (argc != 3) and the code for argv that follows are commented out    
receiver r(io_context, boost::asio::ip::make_address("127.0.0.1"), boost::asio::ip::make_address("127.0.0.1")); //

抛出此错误:

set_option: The requested address is not valid in its context

我尝试了"0.0.0.0"和"127.0.0.1"以及其他值.仍然会出现相同的错误.有人可以帮我弄清楚哪里出了问题吗?

I have tried "0.0.0.0" and "127.0.0.1" and other values. Still get the same error. Could someone please help me figuring out what went wrong?

为澄清起见,这是我使用的代码:

To clarify things, this is the code I use:

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <array>
#include <string>
using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;

class receiver
{
private:
    boost::asio::ip::udp::socket socket_;
    boost::asio::ip::udp::endpoint sender_endpoint_;
    std::array<char, 1024> data_;
    short multicast_port = 13000;

public:
    receiver(boost::asio::io_context& io_context,
        const boost::asio::ip::address& listen_address,
        const boost::asio::ip::address& multicast_address)
        : socket_(io_context)
    {
        // Create the socket so that multiple may be bound to the same address.
        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));

        do_receive();
    }

private:
    void do_receive()
    {
        socket_.async_receive_from(
            boost::asio::buffer(data_), sender_endpoint_,
            [this](boost::system::error_code ec, std::size_t length)
        {
            if (!ec)
            {
                std::cout.write(data_.data(), length);
                std::cout << std::endl;

                do_receive();
            }
        });
    }
};

int main(int argc, char* argv[])
{
    try
    {
        boost::asio::io_context io_context;
        receiver r(io_context, boost::asio::ip::make_address("127.0.0.1"), boost::asio::ip::make_address("127.0.0.1"));
        io_context.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }


    return 0;
}    

推荐答案

也许回答时间太久了,但是我遇到了同样的问题,也许其他人也面临着同样的问题.因此,这可能不是最佳的解决方案,但至少是使其工作的某种方式.

Maybe a bit too long ago to answer, but I've had the same problem and maybe someone else faces the same problem. So this is probably not the best solution, but at least some way to get it to work.

基本上,您使用以太网适配器IP加入多播组,这似乎比在您正在侦听的端口上打开所有以太网适配器更为自然. 请小心使用有效的IP,因为boost不会抛出异常.到目前为止,我发现的唯一解决方案是在接收器方法调用周围使用try/catch.

Basically you join the multicast group with the ethernet adapters IP, which seems more natural than opening all ethernet adapters on the port you're listening to. Be careful to use a valid IP because boost throws an exception if not. The only solution I found so far is using a try / catch around the receiver method call.

当您尝试加入多播组时,似乎您需要一个以太网适配器的IP地址.我建议对其进行编辑并使用静态IP地址(系统">网络">以太网连接").

When you try to join a multicast group it seems you need the IP address of one of your ethernet adapters. I recommend editing it and use a static IP address (System > Network > Ethernet Connections).

对于代码,只需将此行更改为:

As for the code, only this line needs to be changed into:

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

我不确定是否需要.to_v4(),但并没有受到伤害.

I'm not sure if the .to_v4() is needed but it didn't hurt.

这篇关于Boost UDP多播接收器:set_option:请求的地址在其上下文中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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