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:
- Transmit a broadcast packet (255.255.255.255) from the PC containing a specified command (from source port HOST_PORT, destination port DEVICE_PORT).
- 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.
- 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)