esp32 udp 客户端 广播
#include "bsp_udpc.h"
#include "bsp_udpc.h"
#define TAG "bsp_udpc"#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>#define CONFIG_EXAMPLE_IPV4 1void udpcSend(uint8_t *buf, int len)
{int Port = 13334;char ip[16] = "255.255.255.255";struct sockaddr_in dest_addr;dest_addr.sin_addr.s_addr = inet_addr(ip);dest_addr.sin_family = AF_INET;dest_addr.sin_port = htons(Port);int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);if (sock < 0){ESP_LOGE(TAG, "无法创建套接字: errno %d", errno);return;}int err = sendto(sock, buf, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));if (err < 0){ESP_LOGE(TAG, "发送失败: errno %d", errno);}close(sock);
}