c语言实现由网络地址获取ip

#include 
#include 
#include
#include 

int main(){
			
				struct addrinfo *res;
				struct sockaddr_in *sinp;
				const char *addr;
				char abuf[1024];

	        if(getaddrinfo("WWW.baidu.com","http",NULL,&res)==-1)  
        {  
            printf("error to get IP from host name");  
        }else{
					sinp = (struct sockaddr_in *)res->ai_addr;
					addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, 1024);
					printf(" address %s", addr?addr:"unknown");
					printf(" port %d", ntohs(sinp->sin_port));
				}

				return 0;
}



参考Z:http://blog.chinaunix.net/uid-26583794-id-3167485.html

你可能感兴趣的:(c语言实现由网络地址获取ip)