make the mbed take the role of broadcasting

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of BroadcastSend by mbed official

main.cpp

Committer:
shiyilei
Date:
2014-10-30
Revision:
3:2d6cf1043ca6
Parent:
0:e18c3e98ed3d

File content as of revision 3:2d6cf1043ca6:

/**********************************************************
*file:udp broadcast
*Creator:JacobShi
*Time:2014/10/30
* Description:make the mbed take the role of broadcasting
 *********************************************************/

#include "mbed.h"
#include "EthernetInterface.h"
const int BROADCAST_PORT = 8080;
char data_buffer[100];

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("%s\n",eth.getIPAddress() );
    UDPSocket sock;
    sock.init();
    sock.set_broadcasting();
    Endpoint broadcast;
    broadcast.set_address("255.255.255.255", BROADCAST_PORT);
        sock.sendTo(broadcast, "Hello", sizeof("Hello"));

    while (true) {
      int n=sock.receiveFrom(broadcast,data_buffer,sizeof(data_buffer));
      data_buffer[n]='\0';
      printf("%s\n",data_buffer ); 
        
    }
    }