标签:
#include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") int main(int argc, char *argv[]) { WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); char webSite[80] = {0}; puts("Enter Website:"); gets(webSite); struct hostent *host = gethostbyname(webSite); if(!host) { puts("Get IP address error!"); exit(0); } //别名 int i; for(i = 0; host->h_aliases[i]; i++) { printf("Aliases %d: %s.\n", i+1, host->h_aliases[i]); } //地址类型 printf("Address type: %s\n",(host->h_addrtype == AF_INET) ? "AF_INET" : "AF_INET6"); //IP地址 for(i = 0; host->h_addr_list[i]; i++) { printf("IP addr %d: %s\n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i])); } /****************获取本机IP地址*******************/ //获取本机名称 char szHost[256]; gethostname(szHost, 256); //通过本机名称获取本机地址信息 HOSTENT *pHost = gethostbyname(szHost); if(pHost!=NULL) { //遍历并打印本地所有IP地址 for(i = 0; pHost->h_addr_list[i]; i++) { struct in_addr *addr = (struct in_addr*)*(pHost->h_addr_list); printf("本机IP %d: %s\n", i+1, inet_ntoa(addr[i])); } } return 0; }
标签:
原文地址:http://my.oschina.net/u/241930/blog/530410