First commit
Dependencies: EthernetInterface FXAS21002 FXOS8700 NetworkAPI mbed-rtos mbed
Diff: main.cpp
- Revision:
- 8:d55cac25e637
- Parent:
- 6:33b57f606f2b
- Child:
- 9:a4c85bea2d77
--- a/main.cpp Tue Jul 17 18:32:46 2012 +0000 +++ b/main.cpp Wed Jul 18 11:24:12 2012 +0000 @@ -2,6 +2,7 @@ #include "EthernetInterface.h" #include "NetworkAPI/ip/address.hpp" +#include "NetworkAPI/udp/socket.hpp" int main() @@ -10,10 +11,43 @@ interface.init(); interface.connect(); printf("IP Address is %s\n\r", interface.getIPAddress()); - - network::ip::Address address; - address.fromHostname("www.google.nl"); + + network::udp::Socket socket; + char buffer[1024]; + int result = 0; + + if (socket.open() < 0) { + printf("Failed to open UDP Socket\n\r"); + return -1; + } + + if (socket.bind(42) < 0) { + printf("Failed to bind UDP Socket to port 42\n\r"); + } - printf("Network address %s\n", address.toString().c_str()); + while (true) { + result = socket.receive(buffer, 1024); + + switch (result) { + case -1: + printf("Failed to read from UDP Socket\n\r"); + return -1; + + case 0: + printf("Nothing received...?\n\r"); + continue; + + default: + printf("Received %d bytes from %s:%d\n\r", result, + socket.getRemoteEndpoint().getAddress().toString().c_str(), + socket.getRemoteEndpoint().getPort()); + + if (!socket.getRemoteEndpoint().getAddress().isEmpty()) { + socket.send(buffer, result, socket.getRemoteEndpoint()); + } + continue; + } + } + return 0; } \ No newline at end of file