设为首页 加入收藏

TOP

ESP32-两种有趣的wifi连接方式(四)
2023-07-23 13:31:13 】 浏览:328
Tags:ESP32- wifi 连接方
%d", errno); break; } ESP_LOGI(TAG, "Socket created"); int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (err < 0) { ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); } ESP_LOGI(TAG, "Socket bound, port %d", DNS_PORT); while (1) { ESP_LOGI(TAG, "Waiting for data"); struct sockaddr_in6 source_addr; // 足够大,同时适用于IPv4或IPv6 socklen_t socklen = sizeof(source_addr); int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen); // 接收时发生错误 if (len < 0) { ESP_LOGE(TAG, "recvfrom failed: errno %d", errno); close(sock); break; } // 收到的数据 else { // 获取发件人的ip地址为字符串 if (source_addr.sin6_family == PF_INET) { inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); } else if (source_addr.sin6_family == PF_INET6) { inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1); } // null终止我们收到的任何东西,并将其视为字符串… rx_buffer[len] = 0; char reply[DNS_MAX_LEN]; int reply_len = parse_dns_request(rx_buffer, len, reply, DNS_MAX_LEN); ESP_LOGI(TAG, "Received %d bytes from %s | DNS reply with len: %d", len, addr_str, reply_len); if (reply_len <= 0) { ESP_LOGE(TAG, "Failed to prepare a DNS reply"); } else { int err = sendto(sock, reply, reply_len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr)); if (err < 0) { ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); break; } } } } if (sock != -1) { ESP_LOGE(TAG, "Shutting down socket"); shutdown(sock, 0); close(sock); } } vTaskDelete(NULL); } void dns_server_start(void) { xTaskCreate(dns_server_task, "dns_server", 4096, NULL, 5, NULL); }

TCP服务器

/*
 * @Author: tangwc
 * @Date: 2022-10-12 11:55:42
 * @LastEditors: tangwc
 * @LastEditTime: 2022-11-20 16:24:59
 * @Description:
 * @FilePath: \esp32_wifi_link\components\web_server\web_server.c
 *
 *  Copyright (c) 2022 by tangwc, All Rights Reserved.
 */
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>

#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"

#include "esp_wifi.h"
#include "esp_netif.h"
#include "lwip/inet.h"

#include "web_server.h"
#include "wifi_station.h"
#include "url.h"

static const char *TAG = "WEB_SERVER";

#define BUFF_SIZE 1024

httpd_handle_t server = NULL;
extern SemaphoreHandle_t ap_sem;
extern SemaphoreHandle_t dns_sem;
extern const char index_root_start[] asm("_binary_index_html_start");
extern const char index_root_end[] asm("_binary_index_html_end");

/**
 * @description: 一个HTTP GET处理程序 用来发送网页到服务器
 * @param {httpd_req_t} *req
 * @return {*}
 */
static esp_err_t root_get_handler(httpd_req_t *req)
{
    const uint32_t root_len = index_root_end - index_root_start;

    ESP_LOGI(TAG, "Serve html");
    httpd_resp_set_type(req, "text/html");
    httpd_resp_send(req, index_root_start, root_len);

    return ESP_OK;
}

static const httpd_uri_t root = {
    .uri = "/", //根地址默认 192.168.4.1
    .method = HTTP_GET,
    .handler =
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇OpenOCD + DAP-LINK调试ESP32的失.. 下一篇预编译#error的使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目