esp32 udp 客户端 广播
·
esp32 udp 客户端 广播
#include "bsp_udpc.h"
// #include "com_config.h"
// #include "com_xqueue.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 "protocol_examples_common.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>
#define CONFIG_EXAMPLE_IPV4 1
void udpcSend(uint8_t *buf, int len)
{
// if (!G.ethConfig.link)
// {
// ESP_LOGI(TAG, "链路还没准备好");
// return;
// }
// ESP_LOGI(TAG, "创建插座11");
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;
}
// ESP_LOGI(TAG, "创建插座");
int err = sendto(sock, buf, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0)
{
ESP_LOGE(TAG, "发送失败: errno %d", errno);
}
// ESP_LOGI(TAG, "成功发送到 %s:%d", G.udpcConfig.ip, G.udpcConfig.Port);
close(sock);
}
更多推荐



所有评论(0)