I have a dual multicast setup where each multicast group needs to connect to a specific interface on my server.
#include <boost/asio.hpp>
#include <iostream>
namespace ip = boost::asio::ip;
using ip::udp;
boost::asio::io_service io;
struct Connection {
int32_t timeout = 5000;
udp::socket sock {io};
ip::address addr;
bool Connect(std::string const& localAddr, std::string const& addrStr, int port, boost::system::error_code& ec) {
// Multicast socket
udp::endpoint local(ip::address::from_string(localAddr), port); // leaving host/port unbound doesn't seem to help
std::cout << "Using local " << local << "\n";
addr = ip::address::from_string(addrStr);
udp::endpoint multicastEndpoint(addr, port);
sock.open(multicastEndpoint.protocol());
// The commented flags don't seem to have any effect on the findings
//sock.set_option(ip::multicast::enable_loopback());
sock.bind(local, ec);
sock.set_option(ip::multicast::join_group(addr.to_v4()));
sock.set_option(udp::socket::reuse_address(true));
//setsockopt(sock.native(), SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
return ec? false : true;
}
};
struct ConnectionPair {
Connection a, b;
bool Connect(std::string const& addrStrA, int portA, std::string const& addrStrB, int portB, boost::system::error_code& ec) {
// Example adresses; Replace with your local adapter addresses
return a.Connect("172.17.0.1", addrStrA, portA, ec)
&& b.Connect("192.168.195.62", addrStrB, portB, ec);
}
};
int main() {
try {
ConnectionPair pair;
boost::system::error_code ec;
// all hosts multicast groups
if (pair.Connect("224.0.0.251", 5656, "224.0.0.1", 5657, ec)) {
std::cout << "Both connected... ";
boost::asio::deadline_timer dlt(io, boost::posix_time::seconds(5));
dlt.wait();
} else {
std::cout << "Connection error: " << ec.message() << "\n";
}
} catch(std::exception const& e) {
std::cout << "Exception: '" << e.what() << "'\n";
}
std::cout << "Bye\n";
}
Problem is, when using this to connect, socket A is getting no data. Using netsh interface ip show join
, it shows that both multicast groups have been joined in the interface corresponding to localAddrB
, instead of each in their place.
When using mdump
to join the multicast groups, each is connecting to the proper location and receiving data.
I can't figure out where I went wrong with my code.