
NuMaker Ethernet TCP
main.cpp@18:65dbb1786dc4, 2019-12-09 (annotated)
- Committer:
- cyliang
- Date:
- Mon Dec 09 10:02:21 2019 +0000
- Revision:
- 18:65dbb1786dc4
- Parent:
- 17:e5e8673e25ae
- Child:
- 19:43ee976c5259
Change the default protocol as HTTP1.1 instead of HTTP1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 0:45a173aa6aa3 | 1 | #include "mbed.h" |
cyliang | 0:45a173aa6aa3 | 2 | #include "EthernetInterface.h" |
cyliang | 0:45a173aa6aa3 | 3 | #include "TCPSocket.h" |
cyliang | 0:45a173aa6aa3 | 4 | |
cyliang | 18:65dbb1786dc4 | 5 | #define USE_HTTP_1_1 |
cyliang | 1:621d4620acd2 | 6 | //#define LOCAL_LAN |
cyliang | 0:45a173aa6aa3 | 7 | |
cyliang | 0:45a173aa6aa3 | 8 | namespace { |
cyliang | 0:45a173aa6aa3 | 9 | // Test connection information |
cyliang | 2:291ff97f421e | 10 | #ifndef LOCAL_LAN |
ccli8 |
17:e5e8673e25ae | 11 | const char *HTTP_SERVER_NAME = "www.ifconfig.io"; |
cyliang | 0:45a173aa6aa3 | 12 | #else |
cyliang | 2:291ff97f421e | 13 | const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; |
cyliang | 0:45a173aa6aa3 | 14 | #endif |
cyliang | 2:291ff97f421e | 15 | |
cyliang | 2:291ff97f421e | 16 | #ifndef LOCAL_LAN |
ccli8 |
17:e5e8673e25ae | 17 | const char *HTTP_SERVER_FILE_PATH = "/method"; |
cyliang | 0:45a173aa6aa3 | 18 | const int HTTP_SERVER_PORT = 80; |
cyliang | 2:291ff97f421e | 19 | #else |
ccli8 |
17:e5e8673e25ae | 20 | const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt"; |
cyliang | 0:45a173aa6aa3 | 21 | const int HTTP_SERVER_PORT = 8080; |
cyliang | 2:291ff97f421e | 22 | #endif |
cyliang | 0:45a173aa6aa3 | 23 | |
cyliang | 0:45a173aa6aa3 | 24 | #if defined(TARGET_VK_RZ_A1H) |
cyliang | 0:45a173aa6aa3 | 25 | const int RECV_BUFFER_SIZE = 300; |
cyliang | 0:45a173aa6aa3 | 26 | #else |
cyliang | 0:45a173aa6aa3 | 27 | const int RECV_BUFFER_SIZE = 512; |
cyliang | 0:45a173aa6aa3 | 28 | #endif |
cyliang | 0:45a173aa6aa3 | 29 | |
cyliang | 0:45a173aa6aa3 | 30 | // Test related data |
cyliang | 0:45a173aa6aa3 | 31 | const char *HTTP_OK_STR = "200 OK"; |
ccli8 |
17:e5e8673e25ae | 32 | const char *HTTP_EXPECT_STR = "GET"; |
cyliang | 0:45a173aa6aa3 | 33 | |
cyliang | 0:45a173aa6aa3 | 34 | // Test buffers |
cyliang | 0:45a173aa6aa3 | 35 | char buffer[RECV_BUFFER_SIZE] = {0}; |
cyliang | 0:45a173aa6aa3 | 36 | } |
cyliang | 0:45a173aa6aa3 | 37 | |
cyliang | 0:45a173aa6aa3 | 38 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
cyliang | 0:45a173aa6aa3 | 39 | const char *f = std::search(first, last, s_first, s_last); |
cyliang | 0:45a173aa6aa3 | 40 | return (f != last); |
cyliang | 0:45a173aa6aa3 | 41 | } |
cyliang | 0:45a173aa6aa3 | 42 | |
cyliang | 0:45a173aa6aa3 | 43 | int main() { |
ccli8 |
14:7a9e90b34298 | 44 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
14:7a9e90b34298 | 45 | mbed_stats_heap_t heap_stats; |
ccli8 |
14:7a9e90b34298 | 46 | #endif |
ccli8 |
14:7a9e90b34298 | 47 | |
ccli8 |
16:5df36a9f0f60 | 48 | printf("Start TCP test \r\n"); |
cyliang | 2:291ff97f421e | 49 | |
cyliang | 0:45a173aa6aa3 | 50 | bool result = true; |
cyliang | 2:291ff97f421e | 51 | |
cyliang | 0:45a173aa6aa3 | 52 | EthernetInterface eth; |
cyliang | 2:291ff97f421e | 53 | |
cyliang | 2:291ff97f421e | 54 | eth.connect(); //Use DHCP |
cyliang | 0:45a173aa6aa3 | 55 | |
cyliang | 0:45a173aa6aa3 | 56 | printf("TCP client IP Address is %s\r\n", eth.get_ip_address()); |
cyliang | 0:45a173aa6aa3 | 57 | |
cyliang | 0:45a173aa6aa3 | 58 | TCPSocket sock(ð); |
cyliang | 2:291ff97f421e | 59 | |
cyliang | 0:45a173aa6aa3 | 60 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
cyliang | 0:45a173aa6aa3 | 61 | printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
cyliang | 0:45a173aa6aa3 | 62 | |
cyliang | 0:45a173aa6aa3 | 63 | // We are constructing GET command like this: |
ccli8 |
17:e5e8673e25ae | 64 | #ifndef USE_HTTP_1_1 |
ccli8 |
17:e5e8673e25ae | 65 | // GET http://www.ifconfig.io/method HTTP/1.0\n\n |
cyliang | 0:45a173aa6aa3 | 66 | strcpy(buffer, "GET http://"); |
cyliang | 0:45a173aa6aa3 | 67 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 0:45a173aa6aa3 | 68 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
ccli8 |
17:e5e8673e25ae | 69 | strcat(buffer, " HTTP/1.0\n\n"); |
ccli8 |
17:e5e8673e25ae | 70 | #else |
ccli8 |
17:e5e8673e25ae | 71 | // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n" |
ccli8 |
17:e5e8673e25ae | 72 | strcpy(buffer, "GET "); |
ccli8 |
17:e5e8673e25ae | 73 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
ccli8 |
17:e5e8673e25ae | 74 | strcat(buffer, " HTTP/1.1\r\nHost: "); |
ccli8 |
17:e5e8673e25ae | 75 | strcat(buffer, HTTP_SERVER_NAME); |
ccli8 |
17:e5e8673e25ae | 76 | strcat(buffer, "\r\nConnection: close\r\n\r\n"); |
ccli8 |
17:e5e8673e25ae | 77 | #endif |
ccli8 |
17:e5e8673e25ae | 78 | |
cyliang | 0:45a173aa6aa3 | 79 | // Send GET command |
cyliang | 0:45a173aa6aa3 | 80 | sock.send(buffer, strlen(buffer)); |
cyliang | 0:45a173aa6aa3 | 81 | |
cyliang | 0:45a173aa6aa3 | 82 | // Server will respond with HTTP GET's success code |
cyliang | 0:45a173aa6aa3 | 83 | const int ret = sock.recv(buffer, sizeof(buffer) - 1); |
cyliang | 0:45a173aa6aa3 | 84 | buffer[ret] = '\0'; |
cyliang | 0:45a173aa6aa3 | 85 | |
cyliang | 0:45a173aa6aa3 | 86 | // Find 200 OK HTTP status in reply |
cyliang | 0:45a173aa6aa3 | 87 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
ccli8 |
17:e5e8673e25ae | 88 | // Find "deny" string in reply |
ccli8 |
17:e5e8673e25ae | 89 | bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR)); |
cyliang | 0:45a173aa6aa3 | 90 | |
cyliang | 0:45a173aa6aa3 | 91 | if (!found_200_ok) result = false; |
ccli8 |
17:e5e8673e25ae | 92 | if (!found_expect) result = false; |
cyliang | 0:45a173aa6aa3 | 93 | |
cyliang | 0:45a173aa6aa3 | 94 | printf("HTTP: Received %d chars from server\r\n", ret); |
cyliang | 0:45a173aa6aa3 | 95 | printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
ccli8 |
17:e5e8673e25ae | 96 | printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]"); |
cyliang | 0:45a173aa6aa3 | 97 | printf("HTTP: Received massage:\r\n\r\n"); |
cyliang | 0:45a173aa6aa3 | 98 | printf("%s", buffer); |
cyliang | 0:45a173aa6aa3 | 99 | } |
ccli8 |
14:7a9e90b34298 | 100 | |
cyliang | 0:45a173aa6aa3 | 101 | sock.close(); |
cyliang | 0:45a173aa6aa3 | 102 | eth.disconnect(); |
ccli8 |
14:7a9e90b34298 | 103 | |
ccli8 |
14:7a9e90b34298 | 104 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
14:7a9e90b34298 | 105 | mbed_stats_heap_get(&heap_stats); |
ccli8 |
14:7a9e90b34298 | 106 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
ccli8 |
14:7a9e90b34298 | 107 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
ccli8 |
14:7a9e90b34298 | 108 | #endif |
cyliang | 0:45a173aa6aa3 | 109 | } |