Reputation: 11935
I want to use boost.asio to create a multicast UDP sender. I have a my thread and I want to use boost only for:
socket;
send();
Error Handling;
Can you suggest something?
Upvotes: 1
Views: 2917
Reputation: 19032
This is relatively simple to accomplish. Here's a basic class that handles most of everything you need using synchronous calls:
#include <boost/asio.hpp>
#include <boost/scoped_ptr.hpp>
class MulticastSender
{
public:
MulticastSender(const boost::asio::ip::address& multicast_addr,
const unsigned short multicast_port)
: ep_(multicast_addr, multicast_port)
{
socket_.reset(new boost::asio::ip::udp::socket(svc_, ep_.protocol()));
}
~MulticastSender()
{
socket_.reset(NULL);
}
public:
void send_data(const std::string& msg)
{
socket_->send_to(
boost::asio::buffer(msg.str()), ep_);
}
private:
boost::asio::ip::udp::endpoint ep_;
boost::scoped_ptr<boost::asio::ip::udp::socket> socket_;
boost::asio::io_service svc_;
};
This simple class meets 2 of your 3 requirements (no error handling). To use it, simply create an instance of it in an appropriate place, and your thread implementation just calls MulticastSender::send_data() to send the multicast data to the associated endpoint.
Upvotes: 3
Reputation: 12218
Did you give a try to the samples?
<boost>\libs\asio\example\multicast\
It contains sampples for
receiver.cpp
sender.cpp
Upvotes: 1