If set up a program with boost asio. Broadcasts are working fine, if only one network interface is present. However, if there are more network interfaces each broadcast is being sent on one interface only. The interface changes randomly. As observed by wireshark.
I'd expect each broadcast to go out on every interface.
Who's wrong? Me, boost or my understanding of how to use boost. Well, I'm aware, that the latter is the most probable :).
And how can I get the expected behavior.
int myPort=5000;
boost::asio::io_context io_Context{};
boost::asio::ip::udp::socket socket{io_Context};
std::thread sendWorkerThread;
void SendWorkerStart() {
boost::asio::executor_work_guard<decltype(io_Context.get_executor())> work { io_Context.get_executor() };
io_Context.run();
}
void setupSocket() {
socket.set_option(boost::asio::socket_base::reuse_address(true));
socket.set_option(boost::asio::socket_base::broadcast(true));
boost::system::error_code ec;
socket.bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), myPort), ec);
sendWorkerThread = std::thread(udpSocket_c::SendWorkerStart, this);
SendWorkerStart();
}
void SendBroadcast(UdpMessage_t &&message, int size) {
boost::system::error_code ec;
std::lock_guard<std::mutex> lockGuard(sendMutex);
udp::endpoint senderEndpoint(boost::asio::ip::address_v4::broadcast(), myPort);
socket.async_send_to(boost::asio::buffer(message->data(), size), senderEndpoint,
[this](const boost::system::error_code& error,
std::size_t bytes_transferred) { /* nothing to do */} );
}
Thanks for your help.
Edit: It's now running on Windows, but needs to work also in Linux.
As suggested by Alan Birtles in the comments to the question i found an explanation here: UDP-Broadcast on all interfaces
I solved the issue by iterating over he configured interfaces and sending the broadcast to each networks broadcast address as suggested by the linked answer.