Example reception of broadcast messages

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of BroadcastReceive by Emilio Monti

Legacy Warning

This is an mbed 2 example. To learn more about mbed OS 5, visit the docs.

main.cpp

Committer:
emilmont
Date:
2013-03-01
Revision:
0:28ba970b3e23

File content as of revision 0:28ba970b3e23:

#include "mbed.h"
#include "EthernetInterface.h"

const int BROADCAST_PORT = 58083;

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    
    UDPSocket socket;
    socket.bind(BROADCAST_PORT);
    socket.set_broadcasting();
    
    Endpoint broadcaster;
    char buffer[256];
    while (true) {
        printf("\nWait for packet...\n");
        int n = socket.receiveFrom(broadcaster, buffer, sizeof(buffer));
        buffer[n] = '\0';
        printf("Packet from \"%s\": %s\n", broadcaster.get_address(), buffer);
    }
}