Closed piotrek-szczygiel closed 4 years ago
I would like to send broadcast on all interfaces. I've already tried using 255.255.255.255 IP but that doesn't seem to work.
Also seems like even doing something like 192.168.188.255 doesn't work. So is there no way of sending broadcasts?
Hi, just for me to understand: do you want to send data via broadcast using UDPSender or do you want to receive data sent from somewhere via broadcast using UDPReceiver?
I'd like to send broadcast on all connected interfaces. For example when I'm connected to WiFi and Ethernet, I'd like to send broadcast on both of these interfaces.
Creating broadcasting sockets on every interface looks something like this in boost::asio:
std::vector<ba::ip::udp::socket> sockets {};
// Open one socket for every network interface
ba::io_service io_service;
ba::ip::tcp::resolver resolver(io_service);
ba::ip::tcp::resolver::query query(ba::ip::host_name(), "");
auto it = resolver.resolve(query);
while (it != ba::ip::tcp::resolver::iterator()) {
auto addr = (it++)->endpoint().address();
if (addr.is_v4()) {
ba::ip::udp::socket socket { m_ctx };
boost::system::error_code ec;
socket.open(ba::ip::udp::v4(), ec);
if (ec) {
BOOST_LOG_TRIVIAL(error) << "Unable to create broadcast socket on "
<< addr.to_string() << ": " << ec.message();
} else {
socket.set_option(ba::ip::udp::socket::reuse_address(true));
socket.set_option(ba::socket_base::broadcast(true));
socket.bind(ba::ip::udp::endpoint(addr, 0));
sockets.push_back(std::move(socket));
}
}
}
Also it seems like at the moment there is actually no way to send broadcast at all even when knowing the exact network address. I've tried creating UDPSender with address 192.168.188.255 (my ip was 192.168.188.21 with mask 255.255.255.0), but wireshark didn't intercept anything. I was only able to see any udp traffic when i chose unicast address as an endpoint.
I found something in UDPReceiver class but it is behind a private variable.