Boost C++ Libraries: Ticket #13461: C++ Boost UDP multicast errors out with socket.set_option() https://svn.boost.org/trac10/ticket/13461 <p> I am following an example of Boost UDP multicast sending and receiving here(<a class="ext-link" href="https://stackoverflow.com/questions/12708558/c-multiple-multicast-receiver-with-boost-asio/12749727#12749727"><span class="icon">​</span>https://stackoverflow.com/questions/12708558/c-multiple-multicast-receiver-with-boost-asio/12749727#12749727</a>). I make some modifications to predefine IP and port: </p> <p> #include &lt;ctime&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/array.hpp&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; #include &lt;boost/asio.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;boost/thread.hpp&gt; using boost::asio::ip::udp; using std::cout; using std::cin; using std::endl; </p> <p> void read(boost::asio::ip::udp::socket&amp; socket) { </p> <blockquote> <p> boost::asio::ip::udp::endpoint sender; std::vector&lt;char&gt; buffer; std::size_t bytes_readable = 0; for (int i = 0; i &lt; 3; ++i) { </p> <blockquote> <p> <em> Poll until data is available. while (!bytes_readable) { </em></p> <blockquote> <p> <em> Issue command to socket to get number of bytes readable. boost::asio::socket_base::bytes_readable num_of_bytes_readable(true); socket.io_control(num_of_bytes_readable); </em></p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> <em> Get the value from the command. bytes_readable = num_of_bytes_readable.get(); </em></p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> <em> If there is no data available, then sleep. if (!bytes_readable) { </em></p> <blockquote> <p> boost::this_thread::sleep(boost::posix_time::seconds(1)); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em> Resize the buffer to store all available data. buffer.resize(bytes_readable); </em></p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em> Read available data. socket.receive_from( </em></p> <blockquote> <p> boost::asio::buffer(buffer, bytes_readable), sender); </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em> Extract data from the buffer. std::string message(buffer.begin(), buffer.end()); </em></p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em> Output data. std::cout &lt;&lt; "Received message: "; std::cout &lt;&lt; message &lt;&lt; std::endl; </em></p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> void write(boost::asio::ip::udp::socket&amp; socket, boost::asio::ip::udp::endpoint&amp; destination) { </p> <blockquote> <p> std::string message; for (unsigned int i = 0; i &lt; 3; ++i) { </p> <blockquote> <p> std::ostringstream stream; stream &lt;&lt; i; message = stream.str(); socket.send_to(boost::asio::buffer(message), destination); std::cout &lt;&lt; "Sent message: " &lt;&lt; message &lt;&lt; std::endl; </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> int main() { </p> <blockquote> <p> <em> ============================================================================================================ bool receiver = false; boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1"); unsigned short port = boost::lexical_cast&lt;unsigned short&gt;("13"); </em></p> </blockquote> <blockquote> <p> <em> Create socket. using boost::asio::ip::udp; boost::asio::io_service service; udp::socket socket(service); socket.open(boost::asio::ip::udp::v4()); </em></p> </blockquote> <blockquote> <p> <em> Allow other processes to reuse the address, permitting other processes on </em> the same machine to use the multicast address. socket.set_option(udp::socket::reuse_address(true)); </p> </blockquote> <blockquote> <p> <em> Guarantee the loopback is enabled so that multiple processes on the same </em> machine can receive data that originates from the same socket. socket.set_option(boost::asio::ip::multicast::enable_loopback(true)); socket.bind(udp::endpoint(boost::asio::ip::address_v4::any(), port)); udp::endpoint destination(address, port); </p> </blockquote> <blockquote> <p> <em> Join group. namespace ip = boost::asio::ip; socket.set_option(ip::multicast::join_group(address)); </em> Start read or write loops based on command line options. if (receiver) read(socket); else write(socket, destination); </p> </blockquote> <blockquote> <p> return 0; </p> </blockquote> <p> } </p> <p> An error is thrown here: </p> <blockquote> <p> socket.set_option(ip::multicast::join_group(address)); </p> </blockquote> <p> with this detail: </p> <p> Microsoft C++ exception: boost::exception_detail::clone_impl &gt; at memory location 0x0040F470. occurred </p> <p> Can someone please tell me what went wrong? Thanks. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13461 Trac 1.4.3 Kohei Takahashi Sat, 03 Mar 2018 05:34:15 GMT component changed; owner set https://svn.boost.org/trac10/ticket/13461#comment:1 https://svn.boost.org/trac10/ticket/13461#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">chris_kohlhoff</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">asio</span> </li> </ul> Ticket