make the mbed take the role of broadcasting

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of BroadcastSend by mbed official

Committer:
shiyilei
Date:
Thu Oct 30 17:08:27 2014 +0000
Revision:
3:2d6cf1043ca6
Parent:
0:e18c3e98ed3d
make the mbed take the role of broadcasting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shiyilei 3:2d6cf1043ca6 1 /**********************************************************
shiyilei 3:2d6cf1043ca6 2 *file:udp broadcast
shiyilei 3:2d6cf1043ca6 3 *Creator:JacobShi
shiyilei 3:2d6cf1043ca6 4 *Time:2014/10/30
shiyilei 3:2d6cf1043ca6 5 * Description:make the mbed take the role of broadcasting
shiyilei 3:2d6cf1043ca6 6 *********************************************************/
shiyilei 3:2d6cf1043ca6 7
emilmont 0:e18c3e98ed3d 8 #include "mbed.h"
emilmont 0:e18c3e98ed3d 9 #include "EthernetInterface.h"
shiyilei 3:2d6cf1043ca6 10 const int BROADCAST_PORT = 8080;
shiyilei 3:2d6cf1043ca6 11 char data_buffer[100];
emilmont 0:e18c3e98ed3d 12
emilmont 0:e18c3e98ed3d 13 int main() {
emilmont 0:e18c3e98ed3d 14 EthernetInterface eth;
emilmont 0:e18c3e98ed3d 15 eth.init(); //Use DHCP
emilmont 0:e18c3e98ed3d 16 eth.connect();
shiyilei 3:2d6cf1043ca6 17 printf("%s\n",eth.getIPAddress() );
emilmont 0:e18c3e98ed3d 18 UDPSocket sock;
emilmont 0:e18c3e98ed3d 19 sock.init();
emilmont 0:e18c3e98ed3d 20 sock.set_broadcasting();
emilmont 0:e18c3e98ed3d 21 Endpoint broadcast;
emilmont 0:e18c3e98ed3d 22 broadcast.set_address("255.255.255.255", BROADCAST_PORT);
shiyilei 3:2d6cf1043ca6 23 sock.sendTo(broadcast, "Hello", sizeof("Hello"));
shiyilei 3:2d6cf1043ca6 24
emilmont 0:e18c3e98ed3d 25 while (true) {
shiyilei 3:2d6cf1043ca6 26 int n=sock.receiveFrom(broadcast,data_buffer,sizeof(data_buffer));
shiyilei 3:2d6cf1043ca6 27 data_buffer[n]='\0';
shiyilei 3:2d6cf1043ca6 28 printf("%s\n",data_buffer );
shiyilei 3:2d6cf1043ca6 29
emilmont 0:e18c3e98ed3d 30 }
shiyilei 3:2d6cf1043ca6 31 }