{"id":13582,"date":"2025-04-18T20:32:29","date_gmt":"2025-04-18T12:32:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/13582.html"},"modified":"2025-04-18T20:32:29","modified_gmt":"2025-04-18T12:32:29","slug":"%e3%80%90esp32%e3%80%91esp-idf%e5%bc%80%e5%8f%91-wifi%e5%bc%80%e5%8f%91-udp%e7%94%a8%e6%88%b7%e6%95%b0%e6%8d%ae%e6%8a%a5%e5%8d%8f%e8%ae%ae-udp%e5%ae%a2%e6%88%b7%e7%ab%af%e5%92%8c%e6%9c%8d","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/13582.html","title":{"rendered":"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b"},"content":{"rendered":"<h2>1. \u7b80\u4ecb<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0UDP\u534f\u8bae&#xff08;User Datagram Protocol&#xff09;&#xff0c;\u5168\u79f0\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae&#xff0c;\u5b83\u662f\u4e00\u79cd\u9762\u5411\u975e\u8fde\u63a5\u7684\u534f\u8bae&#xff0c;\u9762\u5411\u975e\u8fde\u63a5\u6307\u7684\u662f\u5728\u6b63\u5f0f\u901a\u4fe1\u524d\u4e0d\u5fc5\u4e0e\u5bf9\u65b9\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c; \u4e0d\u7ba1\u5bf9\u65b9\u72b6\u6001\u5c31\u76f4\u63a5\u53d1\u9001\u3002\u81f3\u4e8e\u5bf9\u65b9\u662f\u5426\u53ef\u4ee5\u63a5\u6536\u5230\u8fd9\u4e9b\u6570\u636e\u5185\u5bb9&#xff0c;UDP\u534f\u8bae\u65e0\u6cd5\u63a7\u5236&#xff0c;\u56e0\u6b64\u8bf4UDP\u534f\u8bae\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u534f\u8bae\u3002UDP\u534f\u8bae\u9002\u7528\u4e8e\u4e00\u6b21\u53ea\u4f20\u9001\u5c11\u91cf\u6570\u636e\u3001\u5bf9\u53ef\u9760\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u5e94\u7528\u73af\u5883\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u56e0\u4e3aUDP\u7684\u6570\u636e\u4f20\u8f93\u4e0d\u4e00\u5b9a\u662f\u4e00\u5bf9\u4e00\u7684&#xff0c;\u6240\u4ee5\u4e5f\u884d\u751f\u51fa<span style=\"background-color:#ffd900\">\u5355\u64ad\u3001\u7ec4\u64ad\u548c\u5e7f\u64ad<\/span>\u7684\u6982\u5ff5\u3002<\/p>\n<p>1.\u00a0\u5355\u64ad<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"188\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-680246595511d.png\" width=\"264\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5355\u64ad&#xff08;unicast&#xff09;&#xff0c;\u662f\u6307\u5c01\u5305\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u7684\u4f20\u8f93\u4e2d&#xff0c;\u76ee\u7684\u5730\u5740\u4e3a\u5355\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u4f20\u8f93\u65b9\u5f0f\u3002\u5b83\u662f\u73b0\u4eca\u7f51\u7edc\u5e94\u7528\u6700\u4e3a\u5e7f\u6cdb&#xff0c;\u901a\u5e38\u6240\u4f7f\u7528\u7684\u7f51\u7edc\u534f\u8bae\u6216\u670d\u52a1\u5927\u591a\u91c7\u7528\u5355\u64ad\u4f20\u8f93&#xff0c;\u4f8b\u5982TCP\u534f\u8bae\u3002<\/p>\n<p>2. \u591a\u64ad<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"171\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-680246596493f.png\" width=\"250\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7ec4\u64ad&#xff08;multicast&#xff09;&#xff0c;\u4e5f\u53eb\u591a\u64ad\u6216\u7fa4\u64ad\u3002 \u6307\u628a\u4fe1\u606f\u540c\u65f6\u4f20\u9012\u7ed9\u4e00\u7ec4\u76ee\u7684\u5730\u5740\u3002\u5b83\u4f7f\u7528\u7b56\u7565\u662f\u6700\u9ad8\u6548\u7684&#xff0c;\u56e0\u4e3a\u6d88\u606f\u5728\u6bcf\u6761\u7f51\u7edc\u94fe\u8def\u4e0a\u53ea\u9700\u4f20\u9012\u4e00\u6b21&#xff0c;\u800c\u4e14\u53ea\u6709\u5728\u94fe\u8def\u5206\u53c9\u7684\u65f6\u5019&#xff0c;\u6d88\u606f\u624d\u4f1a\u88ab\u590d\u5236\u3002<\/p>\n<p>3. \u5e7f\u64ad<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"182\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-68024659732b5.png\" width=\"253\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5e7f\u64ad&#xff08;broadcast&#xff09;&#xff0c;\u662f\u6307\u5c01\u5305\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u4e2d\u4f20\u8f93\u65f6&#xff0c;\u76ee\u7684\u5730\u5740\u4e3a\u7f51\u7edc\u4e2d\u6240\u6709\u8bbe\u5907\u7684\u4e00\u79cd\u4f20\u8f93\u65b9\u5f0f\u3002<\/p>\n<h2>2. lwIP<\/h2>\n<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 ESP-IDF\u4f7f\u7528lwIP\u5e93\u5b9e\u73b0TCP\/IP\u534f\u8bae\u6808&#xff0c;\u8fd9\u4e2a\u5e93\u5728\u5927\u591a\u6570\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u90fd\u6709\u7528\u5230&#xff0c;\u5b83\u662f\u4e00\u4e2a\u8f7b\u91cf\u7684TCP\/IP\u534f\u8bae\u5c42\u5957\u4ef6\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u652f\u6301\u4ee5\u4e0b\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#494949\">IP (Internet Protocol, IPv4 and IPv6) including packet forwarding over multiple network interfaces<\/span><\/li>\n<li><span style=\"color:#494949\">ICMP (Internet Control Message Protocol) for network maintenance and debugging<\/span><\/li>\n<li><span style=\"color:#494949\">IGMP (Internet Group Management Protocol) for multicast traffic management<\/span><\/li>\n<li><span style=\"color:#494949\">MLD (Multicast listener discovery for IPv6). Aims to be compliant with RFC 2710. No support for MLDv2<\/span><\/li>\n<li><span style=\"color:#494949\">ND\u00a0(Neighbor discovery and stateless address autoconfiguration for IPv6). Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 (Address autoconfiguration)<\/span><\/li>\n<li><span style=\"color:#494949\">DHCP, AutoIP\/APIPA (Zeroconf) and (stateless) DHCPv6<\/span><\/li>\n<li><span style=\"color:#494949\">UDP (User Datagram Protocol) including experimental UDP-lite extensions<\/span><\/li>\n<li><span style=\"color:#494949\">TCP (Transmission Control Protocol) with congestion control, RTT estimation fast recovery\/fast retransmit and sending SACKs<\/span><\/li>\n<li><span style=\"color:#494949\">raw\/native API for enhanced performance<\/span><\/li>\n<li><span style=\"color:#494949\">Optional Berkeley-like socket API<\/span><\/li>\n<li><span style=\"color:#494949\">TLS: optional layered TCP (&#034;altcp&#034;) for nearly transparent TLS for any<\/span><\/li>\n<li><span style=\"color:#494949\">TCP-based protocol (ported to mbedTLS) (see changelog for more info)<\/span><\/li>\n<li><span style=\"color:#494949\">PPPoS and PPPoE (Point-to-point protocol over Serial\/Ethernet)<\/span><\/li>\n<li><span style=\"color:#494949\">DNS (Domain name resolver incl. mDNS)<\/span><\/li>\n<li><span style=\"color:#494949\">6LoWPAN (via IEEE 802.15.4, BLE or ZEP)<\/span><\/li>\n<\/ul>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u652f\u6301\u4ee5\u4e0b\u5e94\u7528\u5c42\u534f\u8bae&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#494949\">HTTP server with SSI and CGI (HTTPS via altcp)<\/span><\/li>\n<li><span style=\"color:#494949\">SNMPv2c agent with MIB compiler (Simple Network Management Protocol), v3 via altcp<\/span><\/li>\n<li><span style=\"color:#494949\">SNTP (Simple network time protocol)<\/span><\/li>\n<li><span style=\"color:#494949\">NetBIOS name service responder<\/span><\/li>\n<li><span style=\"color:#494949\">MDNS (Multicast DNS) responder<\/span><\/li>\n<li><span style=\"color:#494949\">iPerf server implementation<\/span><\/li>\n<li><span style=\"color:#494949\">MQTT client (TLS support via altcp)<\/span><\/li>\n<\/ul>\n<h2>3. \u4f8b\u7a0b<\/h2>\n<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u4f8b\u7a0b\u5206\u522b\u5728ESP32\u4e0a\u5b9e\u73b0TCP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef&#xff0c;\u4f7f\u7528\u7535\u8111\u4f5c\u4e3a\u53e6\u4e00\u65b9\u8fdb\u884c\u7b80\u5355\u901a\u4fe1\u6d4b\u8bd5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u6d4b\u8bd5\u65f6&#xff0c;<span style=\"background-color:#ffd900\">ESP32\u548c\u7535\u8111\u5fc5\u987b\u5904\u4e8e\u540c\u4e00\u5c40\u57df\u7f51<\/span>\u3002\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u7535\u8111\u7aef\u6d4b\u8bd5\u4f7f\u7528\u7684\u4e0a\u4f4d\u673a\u4e3aVOFA&#043;&#xff0c;\u4e0b\u8f7d\u5730\u5740&#xff1a;VOFA&#043;<\/p>\n<h3>3.1 \u53d1\u9001\u7aef<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u4e2a\u4f8b\u7a0b\u914d\u7f6eESP32\u4e3a\u53d1\u9001\u7aef&#xff0c;\u5f53\u8fde\u63a5WiFi\u70ed\u70b9\u6210\u529f\u540e\u4f1a\u5411\u76ee\u6807IP\u6bcf\u96941\u79d2\u53d1\u9001\u4e00\u6bb5\u6570\u636e\u3002<\/p>\n<p>#include &#034;freertos\/FreeRTOS.h&#034;<br \/>\n#include &#034;freertos\/queue.h&#034;<br \/>\n#include &#034;freertos\/semphr.h&#034;<br \/>\n#include &#034;esp_system.h&#034;<br \/>\n#include &#034;esp_wifi.h&#034;<br \/>\n#include &#034;esp_event.h&#034;<br \/>\n#include &#034;esp_log.h&#034;<br \/>\n#include &#034;esp_mac.h&#034;<br \/>\n#include &#034;nvs_flash.h&#034;<br \/>\n#include &#034;sys\/socket.h&#034;<br \/>\n#include &#034;lwip\/err.h&#034;<br \/>\n#include &#034;lwip\/sys.h&#034;<br \/>\n#include &#034;netdb.h&#034;<br \/>\n#include &#034;arpa\/inet.h&#034;<\/p>\n<p>#include &lt;string.h&gt;<\/p>\n<p>#define TAG &#034;app&#034;<br \/>\n#define HOST_IP_ADDR &#034;172.16.10.26&#034;<br \/>\n#define HOST_PORT 20001<\/p>\n<p>static const char *payload &#061; &#034;Hello from ESP32\\\\r\\\\n&#034;;<br \/>\nstatic TaskHandle_t client_task_handle;<\/p>\n<p>static void udp_client_task(void *args)<br \/>\n{<br \/>\n    struct sockaddr_in dest_addr;<br \/>\n    dest_addr.sin_addr.s_addr &#061; inet_addr(HOST_IP_ADDR);<br \/>\n    dest_addr.sin_family &#061; AF_INET;<br \/>\n    dest_addr.sin_port &#061; htons(HOST_PORT);<\/p>\n<p>    int sock &#061; socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);<br \/>\n    if (sock &lt; 0) {<br \/>\n        ESP_LOGE(TAG, &#034;Unable to create socket: errno %d&#034;, errno);<br \/>\n    }<br \/>\n    ESP_LOGI(TAG, &#034;Socket created, sending to %s:%d&#034;, HOST_IP_ADDR, HOST_PORT);<\/p>\n<p>    while (1) {<br \/>\n        int err &#061; sendto(sock, payload, strlen(payload), 0, (struct sockaddr *)&amp;dest_addr, sizeof(dest_addr));<br \/>\n        if (err &lt; 0) {<br \/>\n            ESP_LOGE(TAG, &#034;Error occurred during sending: errno %d&#034;, errno);<br \/>\n            break;<br \/>\n        }<br \/>\n        ESP_LOGI(TAG, &#034;Message sent&#034;);<\/p>\n<p>        vTaskDelay(1000 \/ portTICK_PERIOD_MS);<br \/>\n    }<\/p>\n<p>    close(sock);<br \/>\n    vTaskDelete(NULL);<br \/>\n}<\/p>\n<p>static void wifi_event_handler(void* arg,<br \/>\n                               esp_event_base_t event_base,<br \/>\n                               int32_t event_id,<br \/>\n                               void* event_data)<br \/>\n{<br \/>\n    if (event_base &#061;&#061; IP_EVENT) {<br \/>\n        if (event_id &#061;&#061; IP_EVENT_STA_GOT_IP) {<br \/>\n            xTaskCreate(udp_client_task, &#034;udp_client&#034;, 4096, NULL, 5, &amp;client_task_handle);<br \/>\n        }<br \/>\n    } else if (event_base &#061;&#061; WIFI_EVENT) {<br \/>\n        if (event_id &#061;&#061; WIFI_EVENT_STA_DISCONNECTED) {<br \/>\n            vTaskDelete(client_task_handle);<br \/>\n        } else if (event_id &#061;&#061; WIFI_EVENT_STA_START) {<br \/>\n            esp_wifi_connect();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>int app_main()<br \/>\n{<br \/>\n    \/* \u521d\u59cb\u5316NVS *\/<br \/>\n    esp_err_t ret &#061; nvs_flash_init();<br \/>\n    if (ret &#061;&#061; ESP_ERR_NVS_NO_FREE_PAGES || ret &#061;&#061; ESP_ERR_NVS_NEW_VERSION_FOUND) {<br \/>\n        ESP_ERROR_CHECK(nvs_flash_erase());<br \/>\n        ESP_ERROR_CHECK(nvs_flash_init());<br \/>\n    }<\/p>\n<p>    \/* \u521d\u59cb\u5316WiFi\u534f\u8bae\u6808 *\/<br \/>\n    ESP_ERROR_CHECK(esp_netif_init());<br \/>\n    ESP_ERROR_CHECK(esp_event_loop_create_default());<br \/>\n    esp_netif_create_default_wifi_sta();<\/p>\n<p>    wifi_init_config_t cfg &#061; WIFI_INIT_CONFIG_DEFAULT();<br \/>\n    ESP_ERROR_CHECK(esp_wifi_init(&amp;cfg));<\/p>\n<p>    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,<br \/>\n                                                        ESP_EVENT_ANY_ID,<br \/>\n                                                        &amp;wifi_event_handler,<br \/>\n                                                        NULL,<br \/>\n                                                        NULL));<\/p>\n<p>    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,<br \/>\n                                                        ESP_EVENT_ANY_ID,<br \/>\n                                                        &amp;wifi_event_handler,<br \/>\n                                                        NULL,<br \/>\n                                                        NULL));<\/p>\n<p>    wifi_config_t wifi_config &#061; {<br \/>\n        .sta &#061; {<br \/>\n            .ssid &#061; &#034;QXL&#034;,<br \/>\n            .password &#061; &#034;88888888&#034;,<br \/>\n            .threshold.authmode &#061; WIFI_AUTH_WPA_WPA2_PSK,<br \/>\n        },<br \/>\n    };<\/p>\n<p>    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));<br \/>\n    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &amp;wifi_config));<br \/>\n    ESP_ERROR_CHECK(esp_wifi_start());<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ESP32\u7684WiFi\u9a71\u52a8\u521d\u59cb\u5316\u5728\u524d\u9762\u7684\u6587\u7ae0\u5df2\u7ecf\u6709\u8be6\u7ec6\u7684\u4ecb\u7ecd\u4e86&#xff0c;\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u56e0\u4e3aUDP\u662f\u9762\u5411\u65e0\u8fde\u63a5\u7684&#xff0c;\u6240\u4ee5\u7f16\u7a0b\u4f1a\u76f8\u5bf9\u7b80\u5355&#xff0c;\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<p>1. \u521b\u5efa\u5957\u63a5\u5b57<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528socket\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793aIP\u534f\u8bae&#xff0c;\u8fd9\u91cc\u4f7f\u7528IPv4&#xff0c;\u5bf9\u5e94IP_INET&#xff1b;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793asocket\u7c7b\u578b&#xff0c;UDP\u534f\u8bae\u53ea\u80fd\u586bSOCK_DGRAM&#xff1b;\u7b2c\u4e09\u4e2a\u53c2\u6570\u8868\u793a\u534f\u8bae\u6808\u7c7b\u578b&#xff0c;\u8fd9\u91cc\u586bIPPROTO_IP\u3002\u51fd\u6570\u4f1a\u8fd4\u56de\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>2. \u5957\u63a5\u5b57\u914d\u7f6e&#xff08;\u53ef\u9009&#xff09;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 socket\u7684\u63a5\u6536\u548c\u53d1\u9001\u9ed8\u8ba4\u662f\u963b\u585e\u7684&#xff0c;\u5373\u5982\u679c\u6570\u636e\u4e0d\u5230\u8fbe\u5c31\u4e00\u76f4\u7b49\u5f85&#xff0c;\u6240\u4ee5\u6839\u636e\u5e94\u7528\u7684\u9700\u8981\u8bbe\u7f6e\u63a5\u6536\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528setsockopt\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff1b;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u534f\u8bae\u5c42&#xff0c;\u5fc5\u987b\u586bSOL_SOCKET&#xff1b;\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a\u8bbe\u7f6e\u9879&#xff0c;\u586bSO_RCVTIMEO&#xff0c;\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff0c;\u5f53\u7136\u51fd\u6570\u8fd8\u652f\u6301\u4ee5\u4e0b\u914d\u7f6e\u9879&#xff1a;<\/p>\n<p>#define SO_DEBUG        0x0001 \/* Unimplemented: turn on debugging info recording *\/<br \/>\n#define SO_ACCEPTCONN   0x0002 \/* socket has had listen() *\/<br \/>\n#define SO_DONTROUTE    0x0010 \/* Unimplemented: just use interface addresses *\/<br \/>\n#define SO_USELOOPBACK  0x0040 \/* Unimplemented: bypass hardware when possible *\/<br \/>\n#define SO_LINGER       0x0080 \/* linger on close if data present *\/<br \/>\n#define SO_DONTLINGER   ((int)(~SO_LINGER))<br \/>\n#define SO_OOBINLINE    0x0100 \/* Unimplemented: leave received OOB data in line *\/<br \/>\n#define SO_REUSEPORT    0x0200 \/* Unimplemented: allow local address &amp; port reuse *\/<br \/>\n#define SO_SNDBUF       0x1001 \/* Unimplemented: send buffer size *\/<br \/>\n#define SO_RCVBUF       0x1002 \/* receive buffer size *\/<br \/>\n#define SO_SNDLOWAT     0x1003 \/* Unimplemented: send low-water mark *\/<br \/>\n#define SO_RCVLOWAT     0x1004 \/* Unimplemented: receive low-water mark *\/<br \/>\n#define SO_SNDTIMEO     0x1005 \/* send timeout *\/<br \/>\n#define SO_RCVTIMEO     0x1006 \/* receive timeout *\/<br \/>\n#define SO_ERROR        0x1007 \/* get error status and clear *\/<br \/>\n#define SO_TYPE         0x1008 \/* get socket type *\/<br \/>\n#define SO_CONTIMEO     0x1009 \/* Unimplemented: connect timeout *\/<br \/>\n#define SO_NO_CHECK     0x100a \/* don&#039;t create UDP checksum *\/<br \/>\n#define SO_BINDTODEVICE 0x100b \/* bind to device *\/<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6bd4\u8f83\u5e38\u7528\u7684\u5c31\u662fSO_RCVTIMEO&#xff0c;\u8bbe\u7f6e\u63a5\u6536\u8d85\u65f6&#xff1b;SO_SNDTIMEO&#xff0c;\u8bbe\u7f6e\u53d1\u9001\u8d85\u65f6\u3002<span style=\"background-color:#ffd900\">\u4e0a\u9762\u5e26Unimlemented\u6ce8\u91ca\u7684\u662fESP-IDF\u4e0d\u652f\u6301\u7684\u914d\u7f6e<\/span>\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u6bcf\u79cd\u914d\u7f6e\u9879\u9700\u8981\u4f20\u5165\u7684\u6570\u636e\u662f\u4e0d\u4e00\u6837\u7684&#xff0c;\u7528\u4e4b\u524d\u5efa\u8bae\u53c2\u8003\u5b98\u65b9\u7684\u8bf4\u660e\u6587\u6863\u3002\u8bbe\u7f6e\u8d85\u65f6\u4f20\u5165\u7684\u662fstruct timeval\u8fd9\u4e2a\u7ed3\u6784\u4f53\u3002<\/p>\n<p>struct timeval {<br \/>\ntime_ttv_sec;<br \/>\nsuseconds_ttv_usec;<br \/>\n};<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 tv_sec\u8bbe\u7f6e\u79d2\u6570&#xff0c;tv_usec\u8bbe\u7f6e\u5fae\u79d2\u6570\u3002<\/p>\n<p>3. \u53d1\u9001\u6570\u636e<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528sendto\u51fd\u6570\u3002\u53c2\u65701\u4e3a\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff1b;\u53c2\u65702\u4e3a\u6570\u636e\u6307\u9488&#xff1b;\u53c2\u65703\u4e3a\u6570\u636e\u957f\u5ea6&#xff1b;\u53c2\u65704\u4e3a\u6807\u5fd7\u4f4d&#xff0c;\u6709\u4ee5\u4e0b\u9009\u9879&#xff1a;<\/p>\n<p>#define MSG_PEEK       0x01<br \/>\n#define MSG_WAITALL    0x02<br \/>\n#define MSG_OOB        0x04<br \/>\n#define MSG_DONTWAIT   0x08<br \/>\n#define MSG_MORE       0x10<br \/>\n#define MSG_NOSIGNAL   0x20 <\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u4e9b\u6807\u5fd7\u4f4d\u662f\u53d1\u9001\u548c\u63a5\u6536\u90fd\u652f\u6301\u7684&#xff0c;\u6bd4\u8f83\u5e38\u7528\u7684\u662fMSG_DONTWAIT&#xff0c;\u50cf\u53d1\u9001\u548c\u63a5\u6536\u51fd\u6570\u662f\u963b\u585e\u7684&#xff0c;\u4f7f\u80fd\u8fd9\u4e2a\u6807\u5fd7\u4f4d\u53ef\u4ee5\u8ba9\u51fd\u6570\u7acb\u5373\u8fd4\u56de&#xff0c;\u4e0d\u7b49\u5f85\u6570\u636e\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u53c2\u65705\u4e3a\u76ee\u6807\u5730\u5740\u4fe1\u606f&#xff0c;\u7ed3\u6784\u4f53\u5982\u4e0b&#xff1a;<\/p>\n<p>struct sockaddr_in {<br \/>\n  u8_t            sin_len;<br \/>\n  sa_family_t     sin_family;<br \/>\n  in_port_t       sin_port;<br \/>\n  struct in_addr  sin_addr;<br \/>\n#define SIN_ZERO_LEN 8<br \/>\n  char            sin_zero[SIN_ZERO_LEN];<br \/>\n};<br \/>\n#endif \/* LWIP_IPV4 *\/<\/p>\n<ul>\n<li>sin_len&#xff1a;\u6570\u636e\u957f\u5ea6&#xff08;\u4e00\u822c\u4e0d\u9700\u8981\u586b&#xff09;&#xff1b;<\/li>\n<li>sin_family&#xff1a;\u5957\u63a5\u5b57\u7c7b\u578b&#xff0c;IPv4\u586bAF_INET&#xff0c;IPv6\u586bAF_INET6&#xff0c;\u5176\u4ed6\u586bAF_UNSPEC&#xff1b;<\/li>\n<li>sin_port&#xff1a;\u7aef\u53e3&#xff1b;<\/li>\n<li>sin_zero&#xff1a;\u9884\u7559\u5b57\u8282&#xff08;\u4e0d\u7528\u7ba1&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u53c2\u65706\u4e3a\u76ee\u6807\u5730\u5740\u7ed3\u6784\u4f53\u957f\u5ea6\u3002<\/p>\n<p>4. \u5173\u95ed\u8fde\u63a5<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528close\u51fd\u6570\u3002\u4f20\u5165\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u5373\u53ef\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4f7f\u7528\u4e0a\u4f4d\u673a\u65f6&#xff0c;\u6570\u636e\u5f15\u64ce\u9009\u62e9\u201cRawData\u201d&#xff0c;\u6570\u636e\u63a5\u53e3\u9009\u62e9UDP&#xff0c;\u8fdc\u7a0bIP\u586b\u5199ESP32\u83b7\u53d6\u5230\u7684IP&#xff0c;<span style=\"background-color:#ffd900\">\u672c\u5730\u7aef\u53e3\u8981\u4e0eESP32\u53d1\u9001\u62a5\u6587\u7684\u7aef\u53e3\u4e00\u81f4<\/span>\u3002\u8bbe\u7f6e\u597d\u540e&#xff0c;\u70b9\u51fb\u5de6\u4e0a\u89d2\u7684\u5706\u5f62\u6309\u94ae\u5373\u53ef\u542f\u52a8\u8fde\u63a5&#xff0c;\u5728\u4e0b\u9762\u6587\u672c\u6846\u80fd\u770b\u5230\u63a5\u6536\u5230\u7684\u6d88\u606f\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1368\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-680246598230b.png\" width=\"2160\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"558\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123226-6802465acca33.png\" width=\"1122\" \/><\/p>\n<h3>2.2 \u63a5\u6536\u7aef<\/h3>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8fd9\u4e2a\u4f8b\u7a0b\u521b\u5efa\u4e00\u4e2aUDP\u63a5\u6536\u7aef&#xff0c;\u63a5\u6536\u6240\u6709\u53d1\u9001\u81f3\u6307\u5b9a\u7aef\u53e3\u7684UDP\u5305\u3002\u00a0<\/p>\n<p>#include &#034;freertos\/FreeRTOS.h&#034;<br \/>\n#include &#034;freertos\/queue.h&#034;<br \/>\n#include &#034;freertos\/semphr.h&#034;<br \/>\n#include &#034;esp_system.h&#034;<br \/>\n#include &#034;esp_wifi.h&#034;<br \/>\n#include &#034;esp_event.h&#034;<br \/>\n#include &#034;esp_log.h&#034;<br \/>\n#include &#034;esp_mac.h&#034;<br \/>\n#include &#034;nvs_flash.h&#034;<br \/>\n#include &#034;sys\/socket.h&#034;<br \/>\n#include &#034;lwip\/err.h&#034;<br \/>\n#include &#034;lwip\/sys.h&#034;<br \/>\n#include &#034;netdb.h&#034;<br \/>\n#include &#034;arpa\/inet.h&#034;<\/p>\n<p>#include &lt;string.h&gt;<\/p>\n<p>#define TAG &#034;app&#034;<br \/>\n#define SERVER_PORT 20001<\/p>\n<p>static TaskHandle_t server_task_handle;<\/p>\n<p>static void udp_server_task(void *args)<br \/>\n{<br \/>\n    char rx_buffer[128];<\/p>\n<p>    struct sockaddr_in dest_addr;<br \/>\n    dest_addr.sin_addr.s_addr &#061; htonl(IPADDR_ANY);<br \/>\n    dest_addr.sin_family &#061; AF_INET;<br \/>\n    dest_addr.sin_port &#061; htons(SERVER_PORT);<\/p>\n<p>    int sock &#061; socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);<br \/>\n    if (sock &lt; 0) {<br \/>\n        ESP_LOGE(TAG, &#034;Unable to create socket: errno %d&#034;, errno);<br \/>\n    }<\/p>\n<p>    int opt &#061; 0;<br \/>\n    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &amp;opt, sizeof(int));<\/p>\n<p>    int err &#061; bind(sock, (struct sockaddr *)&amp;dest_addr, sizeof(dest_addr));<br \/>\n    if (err &lt; 0) {<br \/>\n        ESP_LOGE(TAG, &#034;Socket unable to bind: errno %d&#034;, errno);<br \/>\n        goto __exit;<br \/>\n    }<br \/>\n    ESP_LOGI(TAG, &#034;Socket bound to port %d&#034;, SERVER_PORT);<\/p>\n<p>    while (1) {<br \/>\n        struct sockaddr_in source_addr &#061; {0};<br \/>\n        socklen_t socklen &#061; sizeof(source_addr);<br \/>\n        memset(rx_buffer, 0, sizeof(rx_buffer));<br \/>\n        int len &#061; recvfrom(sock, rx_buffer, sizeof(rx_buffer) &#8211; 1, 0, (struct sockaddr *)&amp;source_addr, &amp;socklen);<br \/>\n        if (len &lt; 0) {<br \/>\n            ESP_LOGE(TAG, &#034;recvfrom failed: errno %d&#034;, errno);<br \/>\n        } else {<br \/>\n            ESP_LOGI(TAG, &#034;Received %d bytes from &#034; IPSTR &#034;:%d, data: %s&#034;, len, IP2STR((esp_ip4_addr_t *) &amp;source_addr.sin_addr), source_addr.sin_port, rx_buffer);<br \/>\n        }<br \/>\n    }<\/p>\n<p>__exit:<br \/>\n    close(sock);<br \/>\n    vTaskDelete(NULL);<br \/>\n}<\/p>\n<p>static void wifi_event_handler(void* arg,<br \/>\n                               esp_event_base_t event_base,<br \/>\n                               int32_t event_id,<br \/>\n                               void* event_data)<br \/>\n{<br \/>\n    if (event_base &#061;&#061; IP_EVENT) {<br \/>\n        if (event_id &#061;&#061; IP_EVENT_STA_GOT_IP) {<br \/>\n            xTaskCreate(udp_server_task, &#034;udp_server&#034;, 4096, NULL, 5, &amp;server_task_handle);<br \/>\n        }<br \/>\n    } else if (event_base &#061;&#061; WIFI_EVENT) {<br \/>\n        if (event_id &#061;&#061; WIFI_EVENT_STA_DISCONNECTED) {<br \/>\n            vTaskDelete(server_task_handle);<br \/>\n        } else if (event_id &#061;&#061; WIFI_EVENT_STA_START) {<br \/>\n            esp_wifi_connect();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>int app_main()<br \/>\n{<br \/>\n    \/* \u521d\u59cb\u5316NVS *\/<br \/>\n    esp_err_t ret &#061; nvs_flash_init();<br \/>\n    if (ret &#061;&#061; ESP_ERR_NVS_NO_FREE_PAGES || ret &#061;&#061; ESP_ERR_NVS_NEW_VERSION_FOUND) {<br \/>\n        ESP_ERROR_CHECK(nvs_flash_erase());<br \/>\n        ESP_ERROR_CHECK(nvs_flash_init());<br \/>\n    }<\/p>\n<p>    \/* \u521d\u59cb\u5316WiFi\u534f\u8bae\u6808 *\/<br \/>\n    ESP_ERROR_CHECK(esp_netif_init());<br \/>\n    ESP_ERROR_CHECK(esp_event_loop_create_default());<br \/>\n    esp_netif_create_default_wifi_sta();<\/p>\n<p>    wifi_init_config_t cfg &#061; WIFI_INIT_CONFIG_DEFAULT();<br \/>\n    ESP_ERROR_CHECK(esp_wifi_init(&amp;cfg));<\/p>\n<p>    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,<br \/>\n                                                        ESP_EVENT_ANY_ID,<br \/>\n                                                        &amp;wifi_event_handler,<br \/>\n                                                        NULL,<br \/>\n                                                        NULL));<\/p>\n<p>    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,<br \/>\n                                                        ESP_EVENT_ANY_ID,<br \/>\n                                                        &amp;wifi_event_handler,<br \/>\n                                                        NULL,<br \/>\n                                                        NULL));<\/p>\n<p>    wifi_config_t wifi_config &#061; {<br \/>\n        .sta &#061; {<br \/>\n            .ssid &#061; &#034;Your SSID&#034;,<br \/>\n            .password &#061; &#034;Your password&#034;,<br \/>\n            .threshold.authmode &#061; WIFI_AUTH_WPA_WPA2_PSK,<br \/>\n        },<br \/>\n    };<\/p>\n<p>    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));<br \/>\n    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &amp;wifi_config));<br \/>\n    ESP_ERROR_CHECK(esp_wifi_start());<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u63a5\u6536\u8005\u7684\u6d41\u7a0b\u548c\u53d1\u9001\u8005\u5dee\u4e0d\u591a&#xff0c;\u4e0b\u9762\u53ea\u4ecb\u7ecd\u6709\u5dee\u5f02\u7684\u90e8\u5206\u3002<\/p>\n<p>1. \u521b\u5efa\u5957\u63a5\u5b57<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53c2\u8003\u524d\u9762\u3002<\/p>\n<p>2. \u914d\u7f6e\u5957\u63a5\u5b57&#xff08;\u53ef\u9009&#xff09;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528setsockopt\u51fd\u6570\u3002\u9664\u4e86\u524d\u9762\u4ecb\u7ecd\u5230\u7684\u914d\u7f6e&#xff0c;UDP\u8fd8\u652f\u6301\u4e00\u4e2aSO_BROADCAST\u7684\u914d\u7f6e&#xff0c;\u7528\u6765\u4f7f\u80fd\u5e7f\u64ad\u5305\u7684\u63a5\u6536&#xff0c;\u6211\u8fd9\u91cc\u662f\u914d\u7f6e\u4e3a\u7981\u7528\u3002<\/p>\n<p>3. \u7ed1\u5b9aIP&#xff08;\u53ef\u9009&#xff09;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u8c03\u7528bind\u51fd\u6570\u3002\u53c2\u65701\u4e3a\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u53c2\u65702\u4e3aIP\u5730\u5740\u7ed3\u6784\u4f53&#xff0c;\u53c2\u65703\u4e3a\u7ed3\u6784\u4f53\u957f\u5ea6\u3002\u901a\u8fc7bind\u53ef\u4ee5\u7ed1\u5b9a\u76d1\u542c\u7684IP\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u53ea\u6709\u63a5\u6536\u5230\u7684\u6570\u636e\u5305\u76ee\u6807IP\u548c\u7aef\u53e3\u4e00\u81f4\u624d\u4f1a\u5904\u7406&#xff0c;\u6211\u8fd9\u91cc\u914d\u7f6e\u63a5\u6536\u4efb\u610fIP&#xff0c;\u7aef\u53e3\u4e3a20001\u3002<\/p>\n<p>4. \u63a5\u6536\u6570\u636e<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8c03\u7528recvfrom\u51fd\u6570\u3002\u4f20\u5165\u53c2\u6570\u548csendto\u4e00\u81f4&#xff0c;\u4e0d\u540c\u7684\u662f\u540e\u9762\u7684IP\u5730\u5740\u7ed3\u6784\u4f53\u662f\u8f93\u51fa\u53c2\u6570&#xff0c;\u4e0d\u9700\u8981\u521d\u59cb\u5316&#xff0c;\u5b83\u4f1a\u8fd4\u56de\u6570\u636e\u5305\u7684\u6e90IP\u4fe1\u606f\u3002<\/p>\n<p>5. \u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u53c2\u8003\u524d\u9762\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e0a\u4f4d\u673a\u7684\u914d\u7f6e\u548c\u524d\u9762\u57fa\u672c\u4e00\u6837&#xff0c;\u552f\u4e00\u7684\u4e0d\u540c\u662f<span style=\"background-color:#ffd900\">\u8fdc\u7a0b\u7aef\u53e3\u8981\u4fdd\u8bc1\u548cESP32\u7ed1\u5b9a\u7684\u7aef\u53e3\u4e00\u81f4<\/span>\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1368\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123227-6802465b1d8ce.png\" width=\"2160\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"507\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123228-6802465c68ba2.png\" width=\"1118\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf25\u6b21\u3002UDP\u534f\u8bae\uff08User Datagram Protocol\uff09\uff0c\u5168\u79f0\uff0c\u5b83\u662f\u4e00\u79cd\uff0c\u9762\u5411\u975e\u8fde\u63a5\u6307\u7684\u662f\u5728\u6b63\u5f0f\u901a\u4fe1\u524d\u4e0d\u5fc5\u4e0e\u5bf9\u65b9\u5148\u5efa\u7acb\u8fde\u63a5\uff0c \u4e0d\u7ba1\u5bf9\u65b9\u72b6\u6001\u5c31\u76f4\u63a5\u53d1\u9001\u3002\u81f3\u4e8e\u5bf9\u65b9\u662f\u5426\u53ef\u4ee5\u63a5\u6536\u5230\u8fd9\u4e9b\u6570\u636e\u5185\u5bb9\uff0cUDP\u534f\u8bae\u65e0\u6cd5\u63a7\u5236\uff0c\u56e0\u6b64\u8bf4UDP\u534f\u8bae\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u534f\u8bae\u3002UDP\u534f\u8bae\u9002\u7528\u4e8e\u4e00\u6b21\u53ea\u4f20\u9001\u5c11\u91cf\u6570\u636e\u3001\u5bf9\u53ef\u9760\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u5e94\u7528\u73af\u5883\u3002\u56e0\u4e3aUDP\u7684\u6570\u636e\u4f20\u8f93\u4e0d\u4e00\u5b9a\u662f\u4e00\u5bf9\u4e00\u7684\uff0c\u6240\u4ee5\u4e5f\u884d\u751f\u51fa\u5355\u64ad\u3001\u7ec4\u64ad\u548c\u5e7f\u64ad\u7684\u6982\u5ff5\u3002\u5355\u64ad\uff08unicast\uff09\uff0c\u662f\u6307\u5c01\u5305\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u7684\u4f20\u8f93\u4e2d\uff0c\u76ee\u7684\u5730\u5740\u4e3a\u5355\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u4f20\u8f93\u65b9\u5f0f\u3002_esp-idf udp<\/p>\n","protected":false},"author":2,"featured_media":13575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[174,179,822,193,270,191],"topic":[],"class_list":["post-13582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-esp32","tag-mcu","tag-udp","tag-270","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/13582.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf25\u6b21\u3002UDP\u534f\u8bae\uff08User Datagram Protocol\uff09\uff0c\u5168\u79f0\uff0c\u5b83\u662f\u4e00\u79cd\uff0c\u9762\u5411\u975e\u8fde\u63a5\u6307\u7684\u662f\u5728\u6b63\u5f0f\u901a\u4fe1\u524d\u4e0d\u5fc5\u4e0e\u5bf9\u65b9\u5148\u5efa\u7acb\u8fde\u63a5\uff0c \u4e0d\u7ba1\u5bf9\u65b9\u72b6\u6001\u5c31\u76f4\u63a5\u53d1\u9001\u3002\u81f3\u4e8e\u5bf9\u65b9\u662f\u5426\u53ef\u4ee5\u63a5\u6536\u5230\u8fd9\u4e9b\u6570\u636e\u5185\u5bb9\uff0cUDP\u534f\u8bae\u65e0\u6cd5\u63a7\u5236\uff0c\u56e0\u6b64\u8bf4UDP\u534f\u8bae\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u534f\u8bae\u3002UDP\u534f\u8bae\u9002\u7528\u4e8e\u4e00\u6b21\u53ea\u4f20\u9001\u5c11\u91cf\u6570\u636e\u3001\u5bf9\u53ef\u9760\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u5e94\u7528\u73af\u5883\u3002\u56e0\u4e3aUDP\u7684\u6570\u636e\u4f20\u8f93\u4e0d\u4e00\u5b9a\u662f\u4e00\u5bf9\u4e00\u7684\uff0c\u6240\u4ee5\u4e5f\u884d\u751f\u51fa\u5355\u64ad\u3001\u7ec4\u64ad\u548c\u5e7f\u64ad\u7684\u6982\u5ff5\u3002\u5355\u64ad\uff08unicast\uff09\uff0c\u662f\u6307\u5c01\u5305\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u7684\u4f20\u8f93\u4e2d\uff0c\u76ee\u7684\u5730\u5740\u4e3a\u5355\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u4f20\u8f93\u65b9\u5f0f\u3002_esp-idf udp\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/13582.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T12:32:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-680246595511d.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13582.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/13582.html\",\"name\":\"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T12:32:29+00:00\",\"dateModified\":\"2025-04-18T12:32:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/13582.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/13582.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13582.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/13582.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.8k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf25\u6b21\u3002UDP\u534f\u8bae\uff08User Datagram Protocol\uff09\uff0c\u5168\u79f0\uff0c\u5b83\u662f\u4e00\u79cd\uff0c\u9762\u5411\u975e\u8fde\u63a5\u6307\u7684\u662f\u5728\u6b63\u5f0f\u901a\u4fe1\u524d\u4e0d\u5fc5\u4e0e\u5bf9\u65b9\u5148\u5efa\u7acb\u8fde\u63a5\uff0c \u4e0d\u7ba1\u5bf9\u65b9\u72b6\u6001\u5c31\u76f4\u63a5\u53d1\u9001\u3002\u81f3\u4e8e\u5bf9\u65b9\u662f\u5426\u53ef\u4ee5\u63a5\u6536\u5230\u8fd9\u4e9b\u6570\u636e\u5185\u5bb9\uff0cUDP\u534f\u8bae\u65e0\u6cd5\u63a7\u5236\uff0c\u56e0\u6b64\u8bf4UDP\u534f\u8bae\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u534f\u8bae\u3002UDP\u534f\u8bae\u9002\u7528\u4e8e\u4e00\u6b21\u53ea\u4f20\u9001\u5c11\u91cf\u6570\u636e\u3001\u5bf9\u53ef\u9760\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u5e94\u7528\u73af\u5883\u3002\u56e0\u4e3aUDP\u7684\u6570\u636e\u4f20\u8f93\u4e0d\u4e00\u5b9a\u662f\u4e00\u5bf9\u4e00\u7684\uff0c\u6240\u4ee5\u4e5f\u884d\u751f\u51fa\u5355\u64ad\u3001\u7ec4\u64ad\u548c\u5e7f\u64ad\u7684\u6982\u5ff5\u3002\u5355\u64ad\uff08unicast\uff09\uff0c\u662f\u6307\u5c01\u5305\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u7684\u4f20\u8f93\u4e2d\uff0c\u76ee\u7684\u5730\u5740\u4e3a\u5355\u4e00\u76ee\u6807\u7684\u4e00\u79cd\u4f20\u8f93\u65b9\u5f0f\u3002_esp-idf udp","og_url":"https:\/\/www.wsisp.com\/helps\/13582.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T12:32:29+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418123225-680246595511d.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/13582.html","url":"https:\/\/www.wsisp.com\/helps\/13582.html","name":"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T12:32:29+00:00","dateModified":"2025-04-18T12:32:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/13582.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/13582.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/13582.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010ESP32\u3011ESP-IDF\u5f00\u53d1 | WiFi\u5f00\u53d1 | UDP\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae + UDP\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4f8b\u7a0b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/13582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=13582"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/13582\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/13575"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=13582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=13582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=13582"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=13582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}