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.

Committer:
Kojto
Date:
Wed May 14 15:39:06 2014 +0000
Revision:
2:d4f436c47a42
Parent:
0:28ba970b3e23
Update to the latest revisions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 0:28ba970b3e23 1 #include "mbed.h"
emilmont 0:28ba970b3e23 2 #include "EthernetInterface.h"
emilmont 0:28ba970b3e23 3
emilmont 0:28ba970b3e23 4 const int BROADCAST_PORT = 58083;
emilmont 0:28ba970b3e23 5
emilmont 0:28ba970b3e23 6 int main() {
emilmont 0:28ba970b3e23 7 EthernetInterface eth;
emilmont 0:28ba970b3e23 8 eth.init(); //Use DHCP
emilmont 0:28ba970b3e23 9 eth.connect();
emilmont 0:28ba970b3e23 10
emilmont 0:28ba970b3e23 11 UDPSocket socket;
emilmont 0:28ba970b3e23 12 socket.bind(BROADCAST_PORT);
emilmont 0:28ba970b3e23 13 socket.set_broadcasting();
emilmont 0:28ba970b3e23 14
emilmont 0:28ba970b3e23 15 Endpoint broadcaster;
emilmont 0:28ba970b3e23 16 char buffer[256];
emilmont 0:28ba970b3e23 17 while (true) {
emilmont 0:28ba970b3e23 18 printf("\nWait for packet...\n");
emilmont 0:28ba970b3e23 19 int n = socket.receiveFrom(broadcaster, buffer, sizeof(buffer));
emilmont 0:28ba970b3e23 20 buffer[n] = '\0';
emilmont 0:28ba970b3e23 21 printf("Packet from \"%s\": %s\n", broadcaster.get_address(), buffer);
emilmont 0:28ba970b3e23 22 }
emilmont 0:28ba970b3e23 23 }