Opened 5 years ago

Last modified 5 years ago

#13461 new Bugs

C++ Boost UDP multicast errors out with socket.set_option()

Reported by: nguyen.tnhoang@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.66.0 Severity: Showstopper
Keywords: udp multicast set_option Cc:

Description

I am following an example of Boost UDP multicast sending and receiving here(https://stackoverflow.com/questions/12708558/c-multiple-multicast-receiver-with-boost-asio/12749727#12749727). I make some modifications to predefine IP and port:

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

void read(boost::asio::ip::udp::socket& socket) {

boost::asio::ip::udp::endpoint sender; std::vector<char> buffer; std::size_t bytes_readable = 0; for (int i = 0; i < 3; ++i) {

Poll until data is available. while (!bytes_readable) {

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);

Get the value from the command. bytes_readable = num_of_bytes_readable.get();

If there is no data available, then sleep. if (!bytes_readable) {

boost::this_thread::sleep(boost::posix_time::seconds(1));

}

}

Resize the buffer to store all available data. buffer.resize(bytes_readable);

Read available data. socket.receive_from(

boost::asio::buffer(buffer, bytes_readable), sender);

Extract data from the buffer. std::string message(buffer.begin(), buffer.end());

Output data. std::cout << "Received message: "; std::cout << message << std::endl;

}

}

void write(boost::asio::ip::udp::socket& socket, boost::asio::ip::udp::endpoint& destination) {

std::string message; for (unsigned int i = 0; i < 3; ++i) {

std::ostringstream stream; stream << i; message = stream.str(); socket.send_to(boost::asio::buffer(message), destination); std::cout << "Sent message: " << message << std::endl;

}

}

int main() {

============================================================================================================ bool receiver = false; boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1"); unsigned short port = boost::lexical_cast<unsigned short>("13");

Create socket. using boost::asio::ip::udp; boost::asio::io_service service; udp::socket socket(service); socket.open(boost::asio::ip::udp::v4());

Allow other processes to reuse the address, permitting other processes on the same machine to use the multicast address. socket.set_option(udp::socket::reuse_address(true));

Guarantee the loopback is enabled so that multiple processes on the same 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);

Join group. namespace ip = boost::asio::ip; socket.set_option(ip::multicast::join_group(address)); Start read or write loops based on command line options. if (receiver) read(socket); else write(socket, destination);

return 0;

}

An error is thrown here:

socket.set_option(ip::multicast::join_group(address));

with this detail:

Microsoft C++ exception: boost::exception_detail::clone_impl > at memory location 0x0040F470. occurred

Can someone please tell me what went wrong? Thanks.

Change History (1)

comment:1 by Kohei Takahashi, 5 years ago

Component: Noneasio
Owner: set to chris_kohlhoff
Note: See TracTickets for help on using tickets.