打印
[DemoCode下载]

m263A的TCP例子

[复制链接]
2178|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
稳稳の幸福|  楼主 | 2021-9-25 23:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题

NuMaker-mbed-wifi-tcp.zip

7.8 MB

使用特权

评论回复
沙发
稳稳の幸福|  楼主 | 2021-9-25 23:27 | 只看该作者
#include <algorithm>
#include "mbed.h"
#include "mbed_stats.h"
#include "TCPSocket.h"

#define MBED_HEAP_STATS_ENABLED 1
#define USE_HTTP_1_1
//#define LOCAL_LAN

namespace {
    // Test connection information
#ifndef LOCAL_LAN
const char *HTTP_SERVER_NAME = "www.ifconfig.io";
#else
const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com";
#endif

#ifndef LOCAL_LAN
const char *HTTP_SERVER_FILE_PATH = "/method";
const int HTTP_SERVER_PORT = 80;
#else
const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/method.txt";
const int HTTP_SERVER_PORT = 8080;
#endif


    const int RECV_BUFFER_SIZE = 512;

    // Test related data
    const char *HTTP_OK_STR = "200 OK";
    const char *HTTP_EXPECT_STR = "GET";

    // Test buffers
    char buffer[RECV_BUFFER_SIZE] = {0};
}

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

int main() {
#if MBED_HEAP_STATS_ENABLED
    mbed_stats_heap_t heap_stats;
#endif

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

    printf("Start WiFi test \r\n");
     
    bool result = true;
    int rc = 0;

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

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

    printf("\n\rUsing WiFi \r\n");
    printf("\n\rConnecting to WiFi..\r\n");
    rc = network_interface->connect();
    if(rc == 0) {
        printf("\n\rConnected to Network successfully\r\n");
    } else {
        printf("\n\rConnection to Network Failed %d! Exiting application....\r\n", rc);
        return 0;
    }   
   
    SocketAddress a;
    network_interface->get_ip_address(&a);
    printf("TCP client IP Address is %s\r\n", a.get_ip_address());
   
    //TCPSocket sock(network_interface);
    TCPSocket sock;
    sock.open(network_interface);

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

        // We are constructing GET command like this:
#ifndef USE_HTTP_1_1
        // GET http://www.ifconfig.io/method HTTP/1.0\n\n
        strcpy(buffer, "GET http://");
        strcat(buffer, HTTP_SERVER_NAME);
        strcat(buffer, HTTP_SERVER_FILE_PATH);
        strcat(buffer, " HTTP/1.0\n\n");      
#else
       // GET /method HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n"
        strcpy(buffer, "GET ");
        strcat(buffer, HTTP_SERVER_FILE_PATH);   
        strcat(buffer, " HTTP/1.1\r\nHost: ");
        strcat(buffer, HTTP_SERVER_NAME);
        strcat(buffer, "\r\nConnection: close\r\n\r\n");
#endif
        
        // Send GET command
        sock.send(buffer, strlen(buffer));

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

        if (!found_200_ok) result = false;
        if (!found_expect) result = false;

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

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

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

    network_interface->disconnect();
    printf(" End \r\n");
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

187

主题

3282

帖子

8

粉丝