マルチキャスト グループのリストに参加し、boost を使用して特定のスレッドで受信データを処理できるマルチキャスト レシーバーを実装する必要があります。私は次のコードを試しました.....
boost::asio::io_service m_io_service;
boost::asio::ip::udp::socket m_multicast_socket(m_io_service);
// listen address
boost::asio::ip::address listen_address
= boost::asio::ip::address::from_string("0.0.0.0");
// listen port
unsigned short multicast_port = m_configuration->m_multicast_interface_port;
boost::asio::ip::udp::endpoint listen_endpoint( listen_address, multicast_port );
// open socket
m_multicast_socket.open( listen_endpoint.protocol() );
// set socket buffer size
m_multicast_socket.set_option(
boost::asio::ip::udp::socket::receive_buffer_size
( m_configuration->m_receiving_socket_buffer_size ) );
// other sockets could bind to listen_address
m_multicast_socket.set_option( boost::asio::ip::udp::socket::reuse_address(true) );
boost::asio::socket_base::bytes_readable num_of_bytes_readable(true);
m_multicast_socket.io_control(num_of_bytes_readable);
m_multicast_socket.bind(listen_endpoint);
// joining a list of multicast group
for ( size_t i=0; i < multicast_groups.size(); ++i )
{
boost::asio::ip::address multicast_address
= boost::asio::ip::address::from_string( multicast_groups[i] );
m_multicast_socket.set_option(
boost::asio::ip::multicast::join_group(
multicast_address ) );
std::cout << multicast_groups[i] << " multicast group joined!" << std::endl;
}
そして、データを読み取るために無限ループ.......
while ( !m_exit )
{
while ( !num_of_bytes_readable.get() )
{
boost::this_thread::sleep( boost::posix_time::milliseconds( 1 ) );
}
boost::asio::ip::udp::endpoint sender_endpoint;
size_t bytes_received = m_multicast_socket.receive_from(
boost::asio::buffer( m_reading_buffer.get(), m_configuration->m_reading_buffer_size )
, sender_endpoint );
if ( bytes_received > 0 )
{
// process
}
boost::this_thread::yield();
}
しかし、データが受信されず、ループします.....
while ( !num_of_bytes_readable.get() )
{
boost::this_thread::sleep( boost::posix_time::milliseconds( 1 ) );
}
決して出ません。
また、boost asio のドキュメントからマルチキャスト レシーバーの例のコードを試してみましたが、async_recv_from
決して返されません。