receive the data from the source and * turn the data back
Dependencies: EthernetInterface mbed-rtos mbed
Fork of BroadcastReceive by
main.cpp@3:689f37688374, 2014-10-30 (annotated)
- Committer:
- shiyilei
- Date:
- Thu Oct 30 17:10:08 2014 +0000
- Revision:
- 3:689f37688374
- Parent:
- 0:28ba970b3e23
receive the data from the source and; * turn the data back
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shiyilei | 3:689f37688374 | 1 | /*************************************************** |
shiyilei | 3:689f37688374 | 2 | *file:broadcast receive |
shiyilei | 3:689f37688374 | 3 | *Creator:JacobShi |
shiyilei | 3:689f37688374 | 4 | *Time:2014/10/30 |
shiyilei | 3:689f37688374 | 5 | *Description:receive the data from the source and |
shiyilei | 3:689f37688374 | 6 | * turn the data back |
shiyilei | 3:689f37688374 | 7 | *******************************************************/ |
shiyilei | 3:689f37688374 | 8 | |
emilmont | 0:28ba970b3e23 | 9 | #include "mbed.h" |
emilmont | 0:28ba970b3e23 | 10 | #include "EthernetInterface.h" |
shiyilei | 3:689f37688374 | 11 | |
shiyilei | 3:689f37688374 | 12 | const int BROADCAST_PORT = 8080; |
shiyilei | 3:689f37688374 | 13 | |
emilmont | 0:28ba970b3e23 | 14 | int main() { |
emilmont | 0:28ba970b3e23 | 15 | EthernetInterface eth; |
emilmont | 0:28ba970b3e23 | 16 | eth.init(); //Use DHCP |
emilmont | 0:28ba970b3e23 | 17 | eth.connect(); |
emilmont | 0:28ba970b3e23 | 18 | |
emilmont | 0:28ba970b3e23 | 19 | UDPSocket socket; |
emilmont | 0:28ba970b3e23 | 20 | socket.bind(BROADCAST_PORT); |
emilmont | 0:28ba970b3e23 | 21 | socket.set_broadcasting(); |
emilmont | 0:28ba970b3e23 | 22 | |
emilmont | 0:28ba970b3e23 | 23 | Endpoint broadcaster; |
emilmont | 0:28ba970b3e23 | 24 | char buffer[256]; |
emilmont | 0:28ba970b3e23 | 25 | while (true) { |
emilmont | 0:28ba970b3e23 | 26 | int n = socket.receiveFrom(broadcaster, buffer, sizeof(buffer)); |
emilmont | 0:28ba970b3e23 | 27 | buffer[n] = '\0'; |
shiyilei | 3:689f37688374 | 28 | socket.sendTo(broadcaster,buffer,sizeof(buffer)); |
shiyilei | 3:689f37688374 | 29 | |
emilmont | 0:28ba970b3e23 | 30 | } |
shiyilei | 3:689f37688374 | 31 | } |