Boost C++ Libraries: Ticket #13477: Initializing boost::asio socket after constructor failed https://svn.boost.org/trac10/ticket/13477 <p> I create a class to broadcast UDP messages as in the attached socket.cpp file. It works fine. Then, I wish to initialize the socket after the class constructor is initialized (to allow user input). So, I change from using socket to a socket pointer as in the socketPtr.cpp. </p> <p> It is a solution suggested by this post (<a class="ext-link" href="https://stackoverflow.com/questions/31371214/initializing-boostasio-sockets-after-constructor"><span class="icon">​</span>https://stackoverflow.com/questions/31371214/initializing-boostasio-sockets-after-constructor</a>). </p> <p> The replacements are as follows, where io_service is wrapped as boost::ref(io_service): </p> <p> <em>boost::asio::ip::udp::socket socket; boost::shared_ptr&lt;udp::socket&gt; socketPtr; </em></p> <p> <em>boost::asio::ip::udp::socket socket(io_service, endpoint.protocol()); </em>socket.set_option(boost::asio::ip::udp::socket::reuse_address(true)); socketPtr = boost::make_shared&lt;udp::socket&gt;(boost::ref(io_service), endpoint.protocol()); socketPtr-&gt;set_option(boost::asio::ip::udp::socket::reuse_address(true)); </p> <p> <em>socket.async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/) socketPtr-&gt;async_send_to(boost::asio::buffer(message), endpoint, [this](boost::system::error_code ec, std::size_t /*length*/) </em></p> <p> Just one thing with the modification: it doesn't work. I keep poring over the code, and could not find the reason why it shouldn't work. Could someone please help? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13477 Trac 1.4.3 nguyen.tnhoang@… Tue, 13 Mar 2018 07:25:07 GMT attachment set https://svn.boost.org/trac10/ticket/13477 https://svn.boost.org/trac10/ticket/13477 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">socket.cpp</span> </li> </ul> Ticket nguyen.tnhoang@… Tue, 13 Mar 2018 07:25:18 GMT attachment set https://svn.boost.org/trac10/ticket/13477 https://svn.boost.org/trac10/ticket/13477 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">socketPtr.cpp</span> </li> </ul> Ticket Krishna Tue, 15 May 2018 03:17:49 GMT <link>https://svn.boost.org/trac10/ticket/13477#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13477#comment:1</guid> <description> <p> I have the same issue. I want to initialize udp socket in my class constructor </p> <hr /> <blockquote> <p> server_udp_socket = boost::asio::ip::udp::socket(*my_io_service, </p> <blockquote> <p> server_endpoint); </p> </blockquote> </blockquote> <hr /> <p> I am seeing the same issue. Is there a solution to this issue, except using initializers ? </p> </description> <category>Ticket</category> </item> </channel> </rss>