Example application that connect with a TCP web server.
Dependencies: mbed mbed-rtos EthernetInterface NetworkAPI
Diff: main.cpp
- Revision:
- 9:4536224842d4
- Parent:
- 8:d55cac25e637
- Child:
- 11:f4d618b8141f
--- a/main.cpp Wed Jul 18 11:24:12 2012 +0000 +++ b/main.cpp Wed Jul 18 13:22:09 2012 +0000 @@ -1,8 +1,9 @@ #include "mbed.h" #include "EthernetInterface.h" +#include "NetworkAPI/buffer.hpp" #include "NetworkAPI/ip/address.hpp" -#include "NetworkAPI/udp/socket.hpp" +#include "NetworkAPI/tcp/socket.hpp" int main() @@ -12,42 +13,33 @@ interface.connect(); printf("IP Address is %s\n\r", interface.getIPAddress()); - network::udp::Socket socket; - char buffer[1024]; - int result = 0; + int result; + + network::tcp::Socket socket; + network::Buffer buffer(256); + std::string request("GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n"); if (socket.open() < 0) { - printf("Failed to open UDP Socket\n\r"); + printf("Failed to open TCP Socket\n\r"); return -1; } - if (socket.bind(42) < 0) { - printf("Failed to bind UDP Socket to port 42\n\r"); + if (socket.connect("mbed.org", 80) < 0) { + printf("Failed to connect with mbed.org\n\r"); + return -1; + } + + if (socket.write((void *)request.data(), request.size()) < 0) { + printf("Failed to write HTTP request\n\r"); + return -1; } - 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; - } - } - + do + { + result = socket.read(buffer); + printf("Received %d bytes:\n\r%s\n\r", result, (char *)buffer.pointer()); + } while(result > 0); + + socket.close(); return 0; } \ No newline at end of file