[DemoCode下载] m263A的TCP例子

[复制链接]
2278|1
 楼主| 稳稳の幸福 发表于 2021-9-25 23:26 | 显示全部楼层 |阅读模式
如题

NuMaker-mbed-wifi-tcp.zip

7.8 MB, 下载次数: 2

 楼主| 稳稳の幸福 发表于 2021-9-25 23:27 | 显示全部楼层
  1. #include <algorithm>
  2. #include "mbed.h"
  3. #include "mbed_stats.h"
  4. #include "TCPSocket.h"

  5. #define MBED_HEAP_STATS_ENABLED 1
  6. #define USE_HTTP_1_1
  7. //#define LOCAL_LAN

  8. namespace {
  9.     // Test connection information
  10. #ifndef LOCAL_LAN
  11. const char *HTTP_SERVER_NAME = "www.ifconfig.io";
  12. #else
  13. const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com";
  14. #endif

  15. #ifndef LOCAL_LAN
  16. const char *HTTP_SERVER_FILE_PATH = "/method";
  17. const int HTTP_SERVER_PORT = 80;
  18. #else
  19. const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt";
  20. const int HTTP_SERVER_PORT = 8080;
  21. #endif


  22.     const int RECV_BUFFER_SIZE = 512;

  23.     // Test related data
  24.     const char *HTTP_OK_STR = "200 OK";
  25.     const char *HTTP_EXPECT_STR = "GET";

  26.     // Test buffers
  27.     char buffer[RECV_BUFFER_SIZE] = {0};
  28. }

  29. bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
  30.     const char *f = std::search(first, last, s_first, s_last);
  31.     return (f != last);
  32. }

  33. int main() {
  34. #if MBED_HEAP_STATS_ENABLED
  35.     mbed_stats_heap_t heap_stats;
  36. #endif

  37. #ifdef MBED_MAJOR_VERSION
  38.     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
  39. #endif

  40.     printf("Start WiFi test \r\n");
  41.      
  42.     bool result = true;
  43.     int rc = 0;

  44.     printf("Start Connection ... \r\n");

  45.     NetworkInterface *network_interface = NetworkInterface::get_default_instance();
  46.     if (NULL == network_interface) {
  47.         printf("NULL network interface! Exiting application....\r\n");
  48.         return 0;
  49.     }

  50.     printf("\n\rUsing WiFi \r\n");
  51.     printf("\n\rConnecting to WiFi..\r\n");
  52.     rc = network_interface->connect();
  53.     if(rc == 0) {
  54.         printf("\n\rConnected to Network successfully\r\n");
  55.     } else {
  56.         printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc);
  57.         return 0;
  58.     }   
  59.    
  60.     SocketAddress a;
  61.     network_interface->get_ip_address(&a);
  62.     printf("TCP client IP Address is %s\r\n", a.get_ip_address());
  63.    
  64.     //TCPSocket sock(network_interface);
  65.     TCPSocket sock;
  66.     sock.open(network_interface);

  67.     printf(" HTTP Connection ... \r\n");
  68.     network_interface->gethostbyname(HTTP_SERVER_NAME, &a);
  69.     a.set_port(HTTP_SERVER_PORT);
  70.     if (sock.connect(a) == 0) {   
  71.         printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);

  72.         // We are constructing GET command like this:
  73. #ifndef USE_HTTP_1_1
  74.         // GET http://www.ifconfig.io/method HTTP/1.0\n\n
  75.         strcpy(buffer, "GET http://");
  76.         strcat(buffer, HTTP_SERVER_NAME);
  77.         strcat(buffer, HTTP_SERVER_FILE_PATH);
  78.         strcat(buffer, " HTTP/1.0\n\n");      
  79. #else
  80.        // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n"
  81.         strcpy(buffer, "GET ");
  82.         strcat(buffer, HTTP_SERVER_FILE_PATH);   
  83.         strcat(buffer, " HTTP/1.1\r\nHost: ");
  84.         strcat(buffer, HTTP_SERVER_NAME);
  85.         strcat(buffer, "\r\nConnection: close\r\n\r\n");
  86. #endif
  87.         
  88.         // Send GET command
  89.         sock.send(buffer, strlen(buffer));

  90.         // Server will respond with HTTP GET's success code
  91.         const int ret = sock.recv(buffer, sizeof(buffer) - 1);
  92.         buffer[ret] = '\0';
  93.         
  94.         // Find 200 OK HTTP status in reply
  95.         bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
  96.         // Find "deny" string in reply
  97.         bool found_expect = find_substring(buffer, buffer + ret, HTTP_EXPECT_STR, HTTP_EXPECT_STR + strlen(HTTP_EXPECT_STR));

  98.         if (!found_200_ok) result = false;
  99.         if (!found_expect) result = false;

  100.         printf("HTTP: Received %d chars from server\r\n", ret);
  101.         printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
  102.         printf("HTTP: Received '%s' status ... %s\r\n", HTTP_EXPECT_STR, found_expect ? "[OK]" : "[FAIL]");
  103.         printf("HTTP: Received massage:\r\n\r\n");
  104.         printf("%s", buffer);
  105.     }

  106. #if MBED_HEAP_STATS_ENABLED
  107.     mbed_stats_heap_get(&heap_stats);
  108.     printf("Current heap: %lu\r\n", heap_stats.current_size);
  109.     printf("Max heap size: %lu\r\n", heap_stats.max_size);
  110. #endif

  111.     printf(" Close socket & disconnect ... \r\n");
  112.     sock.close();

  113.     network_interface->disconnect();
  114.     printf(" End \r\n");
  115. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

207

主题

3457

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部