I'm new to ASIO and I'm trying to get a relatively simple UDP broadcast to work and I'm not seeing any packets leave my PC in Wireshark. Is there a configuraton on the io_service
or socket
that I'm missing? Here's my complete code:
#include <iostream>
#include <boost/asio.hpp>
#include <array>
using boost::asio::ip::udp;
namespace asio = boost::asio;
const char* idnMsg = "*IDN?;";
int main(int argc, char* argv[])
{
try
{
asio::io_service serv;
boost::system::error_code err;
udp::socket socket(serv);
socket.open(asio::ip::udp::v4(), err);
if (!err)
{
socket.set_option(udp::socket::reuse_address(true));
socket.set_option(asio::socket_base::broadcast(true));
asio::ip::udp::endpoint senderEndpoint(asio::ip::address_v4::broadcast(), 7777);
socket.send_to(asio::buffer(idnMsg, 6), senderEndpoint);
//socket.close(err);
std::array<char, 128> buf;
while (socket.available())
{
asio::ip::udp::endpoint remote;
size_t len = socket.receive_from(asio::buffer(buf), remote);
std::cout << "Received: ";
std::cout.write(buf.data(), len);
buf.fill(0);
}
}
else
std::cerr << "Error connecting: " << err.message() << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
You may notice that it bears a striking resemblance to a combination of an asio example and another SO answer.
I'm using Boost.Asio from NuGet in Visual Studio 2015 in Windows 7 x64. If switching to manually-compiled standalone Asio will help, I will. I plan on doing that in the future anyway, as I have no need for the rest of boost in my current plans.
As I said, I saw no packets leave my PC in Wireshark, and I even have several devices on my network that would respond to that packet, and I saw no responses.
EDIT: This code with standalone ASIO in Linux works fine. But I'm going to need ASIO working on Windows if I'm eventually going to be shipping code with it; cross-platform is great and I'm aiming for that, but Windows is the biggest market.