6 years, 11 months ago.

Unable to receive UDP broadcast packets with new networking stack

Greetings.

I've been trying to move to the new networking stack but I'm facing a difficulty. I used to receive UDP broadcast packets with the following code:

void onUDPSocketEvent(UDPSocketEvent e) {
    switch (e) {
        case UDPSOCKET_READABLE:            
            char buf[64] = {0};
            Host host;
            while ( int len = udp.recvfrom( buf, 63, &host ) ) {
                if ( len <= 0 )
                    break;
                led2=!led2;
                pc.printf("From %d.%d.%d.%d (%d): %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], host.getPort(), buf);
                sprintf(buf, "xDev #01");
                int rc = udp.sendto(buf,strlen(buf),&host);
                led2=!led2;
            }
            break;
    }
}

int main() {
...
  Host me(IpAddr(), 7200);

  udp.setOnEvent(&onUDPSocketEvent);

  udp.bind(me);

Now, I've changed to the following:

void broadcast_thread(void const *args) {
    UDPSocket udp;
    udp.bind(7200);

    Endpoint client;
    char buffer[256];
    while (true) {
        printf("\nWait for packet...\n");
        int n = udp.receiveFrom(client, buffer, sizeof(buffer));

        sprintf(buffer, "xDev #01");
        printf("Received packet from: %s\n", client.get_address());
        udp.sendTo(client, buffer, n);
    }

}

int main() {
...
    Thread b_thread(broadcast_thread);

With this new approach, I can only receive packets that are addressed specifically to the mbed IP address. The broadcast packets are not received.

Am I missing something or does the stack do not support the receive of UDP broadcast packets?

Thank you for your help.

Regards,

Pedro Jorge

If you put <<code>> tags around the code it will be easier to read.

posted by Stephen Paulger 04 Jan 2013
Comment on this question

4 Answers

6 years, 11 months ago.

I found the same problem. The UDPsocket wrapper hides the socket options. My solution inherits UDPSocket with modified bind() and init() methods to accept socket option flags. The init() method is untested. The bind method is used as follows :

#include "EthernetInterface.h"
#include "udpBroadcastSocket.h"
..
..
..
UDPBroadcastSocket mySocket;
mySocket.bind(port,SO_BROADCAST);

Import libraryudpBroadcastSocket

Modified wrapper for UDP socket allowing broadcast packets using the new network stack.

Hope this helps. Keith

6 years, 11 months ago.

The new stack doesnt deal with udp broadcast packets this was an issue i ran into also. Adam Green did some mods to the stack for me and made it work i'll try and find the details / code and let you know

all the best

Chris

Hi Chris.

It did work as mentioned. Thank you for the help.

Regards,

Pedro Jorge

posted by Pedro Jorge 04 Jan 2013
6 years, 9 months ago.

We added support and examples for Broadcast and Multicast:

Cheers, Emilio

5 years, 11 months ago.

My program use UDPSocket. I need receive broadcast messages and send broadcast or unicast messages.
With set_broadcasting() i successfully receive and send broadcast messages. If i try send unicast message nothing leave my program ( in accordance with Wireshark log ). How to resolve that problem?

Regards, Andrew