static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
char *use_host = strndup(host, hostlen);
if (!use_host)
{
return NULL;
}
ESP_LOGD(TAG, "host:%s: strlen %lu", use_host, (unsigned long)hostlen);
struct addrinfo *res;
if (getaddrinfo(use_host, NULL, &hints, &res))
{
ESP_LOGE(TAG, "couldn't get hostname for :%s:", use_host);
free(use_host);
return NULL;
}
free(use_host);
return res;
}
static void tcp_client_task(void *pvParameters)
{
char rx_buffer[128];
char addr_str[128];
int addr_family;
int ip_protocol;
char ip[128];
struct addrinfo *cur;
// 域名解析得到ip地址
struct addrinfo *res = resolve_host_name(EXAMPLE_SERVER_URL, strlen(EXAMPLE_SERVER_URL));
if (!res)
{
ESP_LOGI(TAG, "DNS resolve host name failed!");
return;
}
for (cur = res; cur != NULL; cur = cur->ai_next)
{
inet_ntop(AF_INET, &(((struct sockaddr_in *)cur->ai_addr)->sin_addr), ip, sizeof(ip));
ESP_LOGI(TAG, "DNS IP:[%s]", ip);
}
ESP_LOGI(TAG, "Server IP: %s Server Port:%s", ip, EXAMPLE_SERVER_PORT);
}