
NuMaker Ethernet TCP
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "TCPSocket.h" 00004 00005 #define USE_HTTP_1_1 00006 //#define LOCAL_LAN 00007 00008 namespace { 00009 // Test connection information 00010 #ifndef LOCAL_LAN 00011 const char *HTTP_SERVER_NAME = "www.ifconfig.io"; 00012 #else 00013 const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; 00014 #endif 00015 00016 #ifndef LOCAL_LAN 00017 const char *HTTP_SERVER_FILE_PATH = "/method"; 00018 const int HTTP_SERVER_PORT = 80; 00019 #else 00020 const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt"; 00021 const int HTTP_SERVER_PORT = 8080; 00022 #endif 00023 00024 const int RECV_BUFFER_SIZE = 512; 00025 00026 // Test related data 00027 const char *HTTP_OK_STR = "200 OK"; 00028 const char *HTTP_EXPECT_STR = "GET"; 00029 00030 // Test buffers 00031 char buffer[RECV_BUFFER_SIZE] = {0}; 00032 } 00033 00034 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { 00035 const char *f = std::search(first, last, s_first, s_last); 00036 return (f != last); 00037 } 00038 00039 int main() { 00040 #if MBED_HEAP_STATS_ENABLED 00041 mbed_stats_heap_t heap_stats; 00042 #endif 00043 00044 #ifdef MBED_MAJOR_VERSION 00045 printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); 00046 #endif 00047 printf("Start TCP test \r\n"); 00048 00049 bool result = true; 00050 00051 EthernetInterface eth; 00052 00053 eth.connect(); //Use DHCP 00054 00055 #if (MBED_MAJOR_VERSION == 6) 00056 static SocketAddress sa; 00057 eth.get_ip_address(&sa); 00058 printf("TCP client IP Address is %s\r\n", sa.get_ip_address()); 00059 TCPSocket sock; 00060 sock.open(ð); 00061 00062 eth.gethostbyname(HTTP_SERVER_NAME, &sa); 00063 sa.set_port(HTTP_SERVER_PORT); 00064 if (sock.connect(sa) == 0) { 00065 printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); 00066 #else 00067 printf("TCP client IP Address is %s\r\n", eth.get_ip_address()); 00068 00069 TCPSocket sock(ð); 00070 00071 if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { 00072 printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); 00073 #endif 00074 // We are constructing GET command like this: 00075 #ifndef USE_HTTP_1_1 00076 // GET http://www.ifconfig.io/method HTTP/1.0\n\n 00077 strcpy(buffer, "GET http://"); 00078 strcat(buffer, HTTP_SERVER_NAME); 00079 strcat(buffer, HTTP_SERVER_FILE_PATH); 00080 strcat(buffer, " HTTP/1.0\n\n"); 00081 #else 00082 // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n" 00083 strcpy(buffer, "GET "); 00084 strcat(buffer, HTTP_SERVER_FILE_PATH); 00085 strcat(buffer, " HTTP/1.1\r\nHost: "); 00086 strcat(buffer, HTTP_SERVER_NAME); 00087 strcat(buffer, "\r\nConnection: close\r\n\r\n"); 00088 #endif 00089 00090 // Send GET command 00091 sock.send(buffer, strlen(buffer)); 00092 00093 // Server will respond with HTTP GET's success code 00094 const int ret = sock.recv(buffer, sizeof(buffer) - 1); 00095 buffer[ret] = '\0'; 00096 00097 // Find 200 OK HTTP status in reply 00098 bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); 00099 // Find "deny" string in reply 00100 bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR)); 00101 00102 if (!found_200_ok) result = false; 00103 if (!found_expect) result = false; 00104 00105 printf("HTTP: Received %d chars from server\r\n", ret); 00106 printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); 00107 printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]"); 00108 printf("HTTP: Received massage:\r\n\r\n"); 00109 printf("%s", buffer); 00110 } 00111 00112 sock.close(); 00113 eth.disconnect(); 00114 00115 #if MBED_HEAP_STATS_ENABLED 00116 mbed_stats_heap_get(&heap_stats); 00117 printf("Current heap: %lu\r\n", heap_stats.current_size); 00118 printf("Max heap size: %lu\r\n", heap_stats.max_size); 00119 #endif 00120 }
Generated on Tue Jul 12 2022 17:17:32 by
