apr_socket_t * create_mcast_server(apr_pool_t *context, int32_t family, char *mcast_ip, apr_port_t port, char *bind_addr, char *interface) { apr_status_t status = APR_SUCCESS; /* NOTE: If bind is set to mcast_ip in the configuration file, then we will bind the * the multicast address to the socket as well as the port and prevent any * datagrams that might be delivered to this port from being processed. Otherwise, * packets destined to the same port (but a different multicast/unicast channel) will be * processed. */ apr_socket_t *socket = create_udp_server(context, family, port, bind_addr); if(!socket) { return NULL; } /* TODO: We can probe for a list of interfaces and perform multiple join calls for the same * socket to have it listen for multicast traffic on all interfaces (important for * multihomed machines). */ if(interface && !apr_strnatcasecmp(interface, "ALL")) { /* for(each interface) * { * join_mcast(...); * } */ } else { status = join_mcast(context, socket, mcast_ip, port, interface ); } return status == APR_SUCCESS? socket: NULL; }
static void udp_conn(void *pvParameters) { ESP_LOGI(TAG, "task udp_conn start... \n\r"); //等待是否已经成功连接到路由器的标志位 xEventGroupWaitBits(udp_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY); ESP_LOGI(TAG, "esp32 is ready !!! create udp client or connect servece after 5s... \n\r"); vTaskDelay(5000 / portTICK_RATE_MS); //创建客户端并且检查是否创建成功 #if Server_Station_Option ESP_LOGI(TAG, "Now Let us create udp server ... \n\r"); if (create_udp_server() == ESP_FAIL) { ESP_LOGI(TAG, " server create socket error , stop !!! \n\r"); vTaskDelete(NULL); } else { ESP_LOGI(TAG, "server create socket Succeed !!! \n\r"); } #else ESP_LOGI(TAG, "Now Let us create udp client ... \n\r"); if (create_udp_client() == ESP_FAIL) { ESP_LOGI(TAG, "client create socket error , stop !!! \n\r"); vTaskDelete(NULL); } else { ESP_LOGI(TAG, "client create socket Succeed !!! \n\r"); } #endif //创建一个发送和接收数据的任务 TaskHandle_t tx_rx_task; xTaskCreate(&send_recv_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task); //等待 UDP连接成功标志位 xEventGroupWaitBits(udp_event_group, UDP_CONNCETED_SUCCESS, false, true, portMAX_DELAY); int bps; char sendBuff[1024] = "hello xuhong,I am from Esp32 ..."; while (1) { total_data = 0; vTaskDelay(3000 / portTICK_RATE_MS); //时隔三秒发送一次数据 #if !Server_Station_Option send_Buff_with_UDP(sendBuff, 1024); #endif bps = total_data / 3; if (total_data <= 0) { int err_ret = check_connected_socket(); if (err_ret == -1) { ESP_LOGW(TAG, "udp send & recv stop !!! will close socket ... \n\r"); close_socket(); break; } } ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n\r", bps, success_pack); } vTaskDelete(tx_rx_task); vTaskDelete(NULL); }