1.sender
#include <iostream>
#include <string>
#include <sstream>
#include <chrono>
#include <boost/asio.hpp>
class sender {
public:
sender(boost::asio::io_service &context, const boost::asio::ip::address &addr) : endpoint_(addr, multicast_port_),
socket_(context, endpoint_.protocol()),
timer_(context) {
do_send();
}
void set_message_limit(int l) {
message_limit_ = l;
}
void set_multicast_port(int port) {
multicast_port_ = port;
}
private:
void do_send() {
std::ostringstream os;
os << "message" << message_count_++;
message_ = os.str();
socket_.async_send_to(boost::asio::buffer(message_), endpoint_, [this] (boost::system::error_code ec, std::size_t) {
if (!ec && this->message_count_ < message_limit_) {
do_timeout();
}
});
}
void do_timeout() {
timer_.expires_after(std::chrono::seconds(1));
timer_.async_wait([this] (boost::system::error_code ec) {
if (!ec) {
this->do_send();
}
});
}
private:
int message_count_ = 0;
int message_limit_ = 100;
int multicast_port_ = 30001;
std::string message_;
boost::asio::ip::udp::endpoint endpoint_;
boost::asio::ip::udp::socket socket_;
boost::asio::steady_timer timer_;
};
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "usage:./sender 224.0.0.1" << std::endl;
return -1;
}
boost::asio::io_service context;
try {
sender mysender(context, boost::asio::ip::make_address(argv[1]));
context.run();
}
catch(std::exception &e) {
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
}
2.recv.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <array>
#include <chrono>
#include <boost/asio.hpp>
class receiver {
public:
receiver(boost::asio::io_service &context, const boost::asio::ip::address &listen_addr, const boost::asio::ip::address &multicast_addr) : socket_(context) {
boost::asio::ip::udp::endpoint listen_endpoint(listen_addr, multicast_port_);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), multicast_port_));
socket_.set_option(boost::asio::ip::multicast::join_group(multicast_addr));
do_recv();
}
void set_multicast_port(int port) {
multicast_port_ = port;
}
private:
void do_recv() {
socket_.async_receive_from(boost::asio::buffer(buff_), endpoint_, [this] (boost::system::error_code ec, std::size_t len) {
if (!ec) {
std::cout.write(buff_.data(), len);
std::cout << std::endl;
do_recv();
}
});
}
private:
static constexpr size_t BUFF_SIZE = 1024;
private:
int multicast_port_ = 30001;
std::array<char, BUFF_SIZE>buff_;
boost::asio::ip::udp::endpoint endpoint_;
boost::asio::ip::udp::socket socket_;
};
int main(int argc, char **argv) {
if (argc != 3) {
std::cerr << "usage:./recv 192.168.2.109 224.0.0.1" << std::endl;
return -1;
}
boost::asio::io_service context;
try {
receiver myrecv(context,
boost::asio::ip::make_address(argv[1]),
boost::asio::ip::make_address(argv[2])
);
context.run();
}
catch(std::exception &e) {
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
}
3.make.sh
g++ -g -o sender sender.cpp -std=c++11 -I /opt/boost_1_71_0 -pthread -lpthread
g++ -g -o recv recv.cpp -std=c++11 -I /opt/boost_1_71_0 -pthread -lpthread