C++ boost : initializing endpoint after contructor

标签 c++ boost udp boost-asio endpoint

我尝试制作一个使用 UDP 的类。我想在构造函数之后初始化端点,所以我修改如下:

class UdpSender
{
private:
    boost::asio::ip::udp::endpoint endpoint;
    boost::asio::ip::udp::socket socket;

    string multicast_address;
    unsigned short multicast_port;
    boost::thread_group threads;    // thread group
    boost::thread* thread_main;     // main thread
    boost::thread* thread_listen;   // listen thread
    boost::thread* thread_getsend;  // get/send thread
    boost::mutex stopMutex;
    bool initialize = false;
    bool stop, showBroadcast;
    int i_getsend, i_listen, i_main, i_message, interval;
    string message;
public:
    // constructor
    UdpSender(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
        : 
        //endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
        //socket(io_service, endpoint.protocol()),
        multicast_address(multicast_address),
        multicast_port(multicast_port),
        interval(interval),
        showBroadcast(show)
    {
        initialize = true;
        Initialize(io_service);
    }

    UdpSender(boost::asio::io_service& io_service)
    {
        initialize = true;
        Initialize();
    }
    // destructor
    ~UdpSender()
    {
        // show exit message
        cout << "Exiting UDP Communicator." << endl;
    }

    // initialize
    void Initialize(boost::asio::io_service& io_service)
    {
        if (initialize == false)
        {
            GetInfo();
        }

        boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::make_address(multicast_address), multicast_port);
        boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
        socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));   // no need

        thread_main = new boost::thread(boost::ref(*this));
        //thread_listen = new boost::thread(&UdpSender::Callable_Listen, this, interval, boost::ref(i_listen));
        //threads.add_thread(thread_listen);    // listen thread
        thread_getsend = new boost::thread(&UdpSender::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
        threads.add_thread(thread_getsend); // get/send thread
        stop = false;
        i_getsend = 0;
        i_listen = 0;
        i_main = 0;
        i_message = 0;
        message.clear();

        initialize = true;
    }
    // ============ other things removed to shorten code ============
};    

显然,它不喜欢从原始构造函数中注释掉的这两行:

//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),

它也不喜欢新的构造函数:

UdpSender(boost::asio::io_service& io_service)

我该怎么做才能让这一切成为可能?非常感谢你的帮助。谢谢。

最佳答案

没有。 According to the Asio reference ,应通过其 constructors 配置端点对象.

但是,您可以通过[smart-]pointer 持有endpoint,并在UdpSender 构建之后创建它。

// defining:
private:
    boost::shared_ptr<boost::asio::ip::udp::endpoint> endpoint;

// creating in Initialize()
endpoint = boost::make_shared<boost::asio::ip::udp::endpoint>(boost::asio::ip::address::from_string(multicast_address), multicast_port);

关于C++ boost : initializing endpoint after contructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247428/

相关文章:

c++ - 传递回调成员函数的高效方法

c - recvfrom 函数被阻止

c - 如何让消息在 1 秒后被丢弃? (C语言中的UDP客户端/服务器)

c++ - QPainter 初始化画家并使用它

C++ 惰性单例挂起加载

c++ - C++中的模块化编程

c++ - 永远运行 boost asio io_service

python - 如何使用 Python Threading 在线程之间传递参数?

C++编程,迭代器是一个指针,即使它没有被声明为指针

c++ - 有没有办法将整个文件从文件系统微型过滤器驱动程序(内核模式)传递到用户模式应用程序?