make the mbed take the role of broadcasting
Dependencies: EthernetInterface mbed-rtos mbed
Fork of BroadcastSend by
Revision 3:2d6cf1043ca6, committed 2014-10-30
- Comitter:
- shiyilei
- Date:
- Thu Oct 30 17:08:27 2014 +0000
- Parent:
- 2:c6af880e0d98
- Commit message:
- make the mbed take the role of broadcasting
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed May 14 15:36:56 2014 +0000 +++ b/main.cpp Thu Oct 30 17:08:27 2014 +0000 @@ -1,25 +1,31 @@ +/********************************************************** +*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 = 58083; +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); - - char out_buffer[] = "very important data"; - + sock.sendTo(broadcast, "Hello", sizeof("Hello")); + while (true) { - printf("Broadcasting...\n"); - sock.sendTo(broadcast, out_buffer, sizeof(out_buffer)); - Thread::wait(1000); + int n=sock.receiveFrom(broadcast,data_buffer,sizeof(data_buffer)); + data_buffer[n]='\0'; + printf("%s\n",data_buffer ); + } -} + } \ No newline at end of file