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:
Kojto
Date:
2014-05-14
Revision:
2:d4f436c47a42
Parent:
0:28ba970b3e23

File content as of revision 2:d4f436c47a42:

#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);
    }
}