Search code examples
c++socketsudpmulticast

How can I send multicast UDP between 2 servers connected via cross-cable?


We have 2 servers with Solarflare cards directly connected via a cable on an interface (172.16.1.1 and 172.16.1.2). Both servers have other interfaces as well. I'd like test sending UDP multicast packets from one to the other on the 172.16.1.x interface. I've tried the following on the sender:

const auto send_if( "172.16.1.2" );
const auto send_if_addr = boost::asio::ip::address_v4::from_string( send_if );
const boost::asio::ip::udp::endpoint send_if_ep( send_if_addr, 0 );  
// let system select a random port to send data
const auto multicast_addr = boost::asio::ip::address_v4::from_string( "239.6.233.4" );
socket_.set_option( boost::asio::ip::udp::socket::reuse_address( true ) );
socket_.set_option( boost::asio::ip::multicast::outbound_interface( send_if_addr ) ); // send from 115
socket_.bind( send_if_ep );

// based on suggestion
//socket_.set_option( boost::asio::ip::multicast::join_group( send_if_addr ) ); // this throws in boost
socket_.set_option( boost::asio::ip::multicast::join_group( multicast_addr ) );

This does not seem to work. On the other end, socat or tcpdump on the connected interface does not show any multicast data. Will this even work if I don't have a switch or router between the 2 servers on this interface? Join group will never be heard?


Solution

  • It turns out the option hops 0 was set earlier, which was the cultpit of all and I didn't see that. I only need to bind() to the interface before send, without any of the other sets and it works.

    // cultpit
    // socket.set_option( boost::asio::ip::multicast::hops(0) );
    socket_.set_option( boost::asio::ip::udp::socket::reuse_address( true ) );
    socket_.bind( boost::asio::ip::udp::endpoint( 
              boost::asio::ip::address_v4::from_string( "172.16.1.2" ), 0 ) );