2

My apologies if this has already been answered somewhere, but I can't seem to find an appropriate answer here for the life of me.

My problem is this: I'm developing an embedded application with an Ethernet interface. I need to be able to discover these devices on the network and configure their IP's even when I don't know the IP of the device. My intended approach is as follows:

  1. Transmit a broadcast packet (255.255.255.255) from the PC containing a specified command (from source port HOST_PORT, destination port DEVICE_PORT).
  2. Have the device listening for that command in broadcast packets. The device then responds with it's own broadcast packet of it's own from port DEVICE_PORT to port HOST_PORT.
  3. My app recieves the reply packet which contains a bunch of information such as serial numbers and enumerates all the devices on the network.

I've got (1) and (2) working, the problem comes when attempting to recieve the broadcast packet on the PC. I have verified with Wireshark that the packets are actually hitting the network correctly, so it must be something wrong with my usage of boost.

Here's a snippet of the code I'm using:

    using namespace boost::asio;

    io_service my_io_service;                           // Create socket and IO service, bind socket to endpoint.
    ip::udp::socket socket(my_io_service);
    socket.open(ip::udp::v4());

    ip::udp::endpoint localEndpoint(ip::address_v4::any(), HOST_PORT );
    ip::udp::endpoint remoteEndpoint(ip::address_v4::broadcast(), DEVICE_PORT );

    socket.bind( localEndpoint );
    socket.connect( remoteEndpoint );
    socket_base::broadcast option(true);
    socket.set_option(option);


    TXMessage.str("");
    TXMessage << "RECQRE" << (i++);
    socket.send(buffer(TXMessage.str().c_str(), TXMessage.str().length()));
    len = socket.receive(buffer(RXBuffer, (BUFFER_SIZE-1)));

The app simply locks in the socket.recieve(...) call. Can anyone see a glaring issue with this code or configuration? I believe I'm binding and connecting appropriately, and I know I'm setting the 'broadcast' option.

Alternatively, I'm happy to be pointed in the direction of an alternative solution to my device identification problem if you think I've got the wrong end of the stick.

Edit: Things I have already checked:

  • I am binding to INADDR_ANY
  • Windows firewall has been disabled
  • The application has been tested without Wireshark running
  • The remote device is alive and well (I have a serial debugging connection to check this)
Redeye
  • 1,582
  • 1
  • 14
  • 22
OcularProgrammer
  • 482
  • 2
  • 5
  • 19
  • I realize this was about 3 years ago, but I'm having the exact same problem today. [Here is my question](http://stackoverflow.com/q/32917830/425871). Did you ever figure this out? – Steve Oct 03 '15 at 19:49
  • Hi Steve. I have it working, there is one thing you could try which may work. Before you make your boost async_receive_from call, do a TX of something, anything at all. My comments from 3 years ago indicates that this is necessary to force the socket to properly acquire the local port. I'm not sure whether that was the silver bullet, but it's worth a try. – OcularProgrammer Oct 30 '15 at 00:14
  • If I ever get back to it I will be sure to try that. Thanks for the tip! – Steve Oct 30 '15 at 00:56

0 Answers0