Simple HTTP Client Example with HTTPClient class
Dependencies: HTTPClient mbed-rtos mbed-src
Diff: main.cpp
- Revision:
- 0:880477b153ac
- Child:
- 1:24d373f4f0ea
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Nov 29 13:25:45 2014 +0000 @@ -0,0 +1,40 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +int main() { + char send_buff[] = "GET / abcd\r\n\r\n" ; + #define RECV_BUFF 1024 + char recv_buff[RECV_BUFF]; + #define SERVER_IP "192.168.1.12" + int port = 80; + int n ; + + printf("TCPCllient waiting for server IP and port...\r\n"); + + EthernetInterface eth; + eth.init() ; + eth.connect(); + + printf("TCPClient IP Address is %s\r\n", eth.getIPAddress()); + TCPSocketConnection socket; + while (socket.connect(SERVER_IP, port) < 0) { + printf("TCPCllient unable to connect to %s:%d\r\n", SERVER_IP, port); + wait(1.0); + } + if (socket.send(send_buff, sizeof(send_buff)-1) != (sizeof(send_buff)-1)){ + printf("Socket.send failed\n"); + return ; + } + + puts("Server Response:\n") ; + do { + n = socket.receive(recv_buff, sizeof(recv_buff)-1); + if (n >= 0) { + recv_buff[n] = 0; + printf("%s", recv_buff); + } else return ; + } while(n > 0) ; + socket.close(); + eth.disconnect(); + return 0; +} \ No newline at end of file