1

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.

Community
  • 1
  • 1
PrinceBilliard
  • 181
  • 1
  • 17

2 Answers2

1

It looks to me that the best you can expect is one small packet. You should check the return from the send_to call, which will tell you how many bytes were sent.

if( socket.send_to(asio::buffer(idnMsg, 6), senderEndpoint) != 6 )
{  std::cerr << "send_to failed" << std::endl; exit(1); }
else
{ std::cerr << "one packet sent" << std::endl; }

I compiled and ran your code: it works just fine -

enter image description here

Perhaps you missed this single packet in a flood of others?

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • It's returning 6; so Asio claims to have sent 6 bytes. I was using the filter `udp and src or dst port 7777` to see either the packet going out or the response coming back, and it was blank; no packets. I'll try compiling it with standalone Asio and see if that works. – PrinceBilliard Oct 09 '15 at 19:14
1

Answering my own question to provide information to others who may have the same issue. I determined that the culprit was the "VirtualBox Host-Only Network" adapter added by VirtualBox. It looks like it was swallowing up all UDP broadcast packets sent from my machine. Disabling that adapter allowed my Asio code to work, and didn't event interfere with my VMs set to Bridged Adapter.

PrinceBilliard
  • 181
  • 1
  • 17