void DomainToIP(const char * host, const char *portStr, char *ip){ struct addrinfo hints, *res, *res0; char str[32] = {0}; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; int ret = getaddrinfo(host, portStr, &hints, &res0); if (ret != 0) { fprintf(stderr,"getaddrinfo: %s\n", gai_strerror(ret));<pre code_snippet_id="582093" snippet_file_name="blog_20150117_2_5030408" name="code" class="cpp"> return; } for(res = res0; res; res = res->ai_next){ if(res->ai_family == AF_INET){ // Found IPv4 address inet_ntop(AF_INET, &(((struct sockaddr_in *)(res->ai_addr))->sin_addr), str, 32); <span style="font-family: Arial, Helvetica, sans-serif;">printf</span><span style="font-family: Arial, Helvetica, sans-serif;">("解析出来的IP: %s\n", str);</span> strncpy(ip, str, 32); }else if(res->ai_family == AF_INET6){ // Found IPv6 address inet_ntop(AF_INET6, &(((struct sockaddr_in *)(res->ai_addr))->sin_addr), str, 32); printf("解析出来的IP6: %s\n", str); strncpy(ip, str, 32); } } freeaddrinfo(res0); }
时间: 2024-11-06 11:38:07