]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/helloworld/tcpclient/main.cpp
3 #include "EthernetInterface.h"
7 // Test connection information
8 const char *HTTP_SERVER_NAME
= "developer.mbed.org";
9 const char *HTTP_SERVER_FILE_PATH
= "/media/uploads/mbed_official/hello.txt";
10 const int HTTP_SERVER_PORT
= 80;
11 const int RECV_BUFFER_SIZE
= 512;
14 const char *HTTP_OK_STR
= "200 OK";
15 const char *HTTP_HELLO_STR
= "Hello world!";
18 char buffer
[RECV_BUFFER_SIZE
] = {0};
21 bool find_substring(const char *first
, const char *last
, const char *s_first
, const char *s_last
) {
22 const char *f
= std::search(first
, last
, s_first
, s_last
);
27 MBED_HOSTTEST_TIMEOUT(20);
28 MBED_HOSTTEST_SELECT(default_auto
);
29 MBED_HOSTTEST_DESCRIPTION(TCP client hello world
);
30 MBED_HOSTTEST_START("NET_1");
33 EthernetInterface eth
;
34 eth
.init(); //Use DHCP
36 printf("TCP client IP Address is %s\r\n", eth
.getIPAddress());
38 TCPSocketConnection sock
;
39 if (sock
.connect(HTTP_SERVER_NAME
, HTTP_SERVER_PORT
) == 0) {
40 printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME
, HTTP_SERVER_PORT
);
42 // We are constructing GET command like this:
43 // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
44 strcpy(buffer
, "GET http://");
45 strcat(buffer
, HTTP_SERVER_NAME
);
46 strcat(buffer
, HTTP_SERVER_FILE_PATH
);
47 strcat(buffer
, " HTTP/1.0\n\n");
49 sock
.send_all(buffer
, strlen(buffer
));
51 // Server will respond with HTTP GET's success code
52 bool found_200_ok
= false;
54 const int ret
= sock
.receive(buffer
, sizeof(buffer
) - 1);
56 // Find 200 OK HTTP status in reply
57 found_200_ok
= find_substring(buffer
, buffer
+ ret
, HTTP_OK_STR
, HTTP_OK_STR
+ strlen(HTTP_OK_STR
));
58 printf("HTTP: Received %d chars from server\r\n", ret
);
59 printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok
? "[OK]" : "[FAIL]");
60 printf("HTTP: Received massage:\r\n\r\n");
64 // Server will respond with requested file content
65 bool found_hello
= false;
67 const int ret
= sock
.receive(buffer
, sizeof(buffer
) - 1);
69 // Find Hello World! in reply
70 found_hello
= find_substring(buffer
, buffer
+ ret
, HTTP_HELLO_STR
, HTTP_HELLO_STR
+ strlen(HTTP_HELLO_STR
));
71 printf("HTTP: Received %d chars from server\r\n", ret
);
72 printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR
, found_hello
? "[OK]" : "[FAIL]");
73 printf("HTTP: Received massage:\r\n\r\n");
77 if (found_200_ok
&& found_hello
) {
84 MBED_HOSTTEST_RESULT(result
);