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.

Revision:
0:28ba970b3e23
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Mar 01 16:23:51 2013 +0000
@@ -0,0 +1,23 @@
+#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);
+    }
+}