Example reception of broadcast messages

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of BroadcastReceive by Emilio Monti

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 const int BROADCAST_PORT = 58083;
00005 
00006 int main() {
00007     EthernetInterface eth;
00008     eth.init(); //Use DHCP
00009     eth.connect();
00010     
00011     UDPSocket socket;
00012     socket.bind(BROADCAST_PORT);
00013     socket.set_broadcasting();
00014     
00015     Endpoint broadcaster;
00016     char buffer[256];
00017     while (true) {
00018         printf("\nWait for packet...\n");
00019         int n = socket.receiveFrom(broadcaster, buffer, sizeof(buffer));
00020         buffer[n] = '\0';
00021         printf("Packet from \"%s\": %s\n", broadcaster.get_address(), buffer);
00022     }
00023 }