esp32 tcpclient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
int tcp_client()
{
    char rx_buffer[128];
    char host_ip[] = HOST_IP_ADDR;
    int addr_family = 0;
    int ip_protocol = 0;
     while (1)
    {
        struct sockaddr_in dest_addr;
        inet_pton(AF_INET, host_ip, &dest_addr.sin_addr);
        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = htons(CLIENTPORT);
        addr_family = AF_INET;
        ip_protocol = IPPROTO_IP;
        int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
        if (sock < 0)
        {
            ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
            break;
        }
        ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, CLIENTPORT);
 
        int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
        if (err != 0)
        {
            ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
            s_sta_is_connected = false;
            break;
        }
        else
        {
            s_sta_is_connected = true;
            ESP_LOGI(TAG, "Successfully connected");
 
          //  return sock;
        }
        while(1) {
                fd_set reads;
                FD_ZERO(&reads);
                FD_SET(sock, &reads);
 
                struct timeval timeout;
                timeout.tv_sec = 0;
                timeout.tv_usec = 100000;
                if (select(sock+1, &reads, 0, 0, &timeout) < 0) {
                    ESP_LOGI(TAG, "select() failed.\n");
 
                }
                if (FD_ISSET(sock, &reads)) {
                    char read[1024];
                    int bytes_received = recv(sock, read, 1024, 0);
                    if (bytes_received < 1) {
                        printf("Connection closed by peer.\n");
                        break;
                    }
                    printf("Received (%d bytes): %.*s",
                        bytes_received, bytes_received, read);
                }
                 if (FD_ISSET(0, &reads)) {
 
                    char read[1024];
                    if (!fgets(read, 1024, stdin)) break;
                    printf("Sending: %s", read);
                    int bytes_sent = send(sock, read, strlen(read), 0);
                    printf("Sent %d bytes.\n", bytes_sent);
                }
            }
            return sock;
        if (sock != -1)
        {
            ESP_LOGE(TAG, "Shutting down socket and restarting...");
            shutdown(sock, 0);
            close(sock);
            return -1;
        }
    }
    return 0;
}

  

posted on   lydstory  阅读(13)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-09-09 类的抽象能力
2021-09-09 程序员辞职的一万个理由
2020-09-09 vlc libavi
2020-09-09 关键字
2020-09-09 img_yuv422_to_rgb32
2020-09-09 itop4412
2019-09-09 WTSEnumerateSessions 枚举session信息

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示