c/c++实现获取域名的IP地址
c/c++实现获取域名的IP地址
//GetHostIP.cpp:定义控制台应用程序的入口点。 // #include"stdafx.h" #include<winsock2.h> #include<ws2tcpip.h> #include<stdio.h> #include<windows.h> #pragmacomment(lib,"ws2_32.lib") intmain(intargc,char**argv) { //----------------------------------------- //Declareandinitializevariables /* *WSADATA结构被用来储存调用AfxSocketInit全局函数返回的WindowsSockets初始化信息。 *这个结构被用来存储被WSAStartup函数调用后返回的WindowsSockets数据。它包含Winsock.dll执行的数据。 */ WSADATAwsaData; intiResult; DWORDdwError;/*每个word为2个字节的长度,DWORD双字即为4个字节,每个字节是8位*/ inti=0; structhostent*remoteHost;/*域名*/ char*host_name;/*主机名*/ structin_addraddr;/*ip*/ char**pAlias; //Validatetheparameters if(argc!=2){ printf("usage:GetHostIPhostname\n"); return1; } //此处应添加的程序如下 //1.当初始化winsocket iResult=WSAStartup(MAKEWORD(2,2),&wsaData);/*&取地址*/ //2.检查该socket是否初始化成功,即该socket是否等于0;如果初始化不成功,应当给出错误报警,并结束程序。 if(iResult!=0){ printf("初始化失败!\n"); return1; } /////////////////结束/////////////////////////////////// host_name=argv[1]; printf("Callinggethostbynamewith%s\n",host_name); //此处应添加的程序如下 //1.利用函数gethostbyname(),获取给定主机名的指针。 remoteHost=gethostbyname(host_name); //2.应当熟悉该结构指针的结构 //其中该函数申明如下:structhostent*gethostbyname(constchar*name) //此处应添加的程序如下 //1.如果上面函数返回的主机结构指针为空(NULL),则做如下处理: //a.利用函数intWSAGetLastError(void)检查当前是否发生网络错误, //b.返回的发生的错误类型并作相应的处理,比如,若没有找到主机的错误(此时该函数返回WSAHOST_NOT_FOUND) if(remoteHost==NULL){ //printf("gethostbynameError:%d",WSAGetLastError()); return1; }else{ //2.如果返回的主机指针不为空,则做如下处理: //a.打印出如下参数:主机名和IP地址,若该主机对应于多个ip地址,应当分别列出。 printf("主机名:%s\n",remoteHost->h_name); for(i=0;;i++){ if(remoteHost->h_addr_list[i]!=0) { /*从缓存中把p拷贝到addr中 *同时addr.S_un.Saddr *in_addripAddr; *ipAddr.S_un.S_addr=inet_addr("127.0.0.1"); *就是把字符串形式的ip地址转化为0xXXXXXXXX形式的地址格式。 */ addr.s_addr=*(u_long*)remoteHost->h_addr_list[i]; printf("ip#%d:%s\n",i,inet_ntoa(addr));/*inet_ntoa()函数将网络地址转成二进制的数字相关函数:inet_aton,inet_ntoa*/ } /* for(i=0;;i++){ char*p=remoteHost->h_addr_list[i]; if(p==NULL)break; /*从缓存中把p拷贝到addr中 *同时addr.S_un.Saddr *in_addripAddr; *ipAddr.S_un.S_addr=inet_addr("127.0.0.1"); *就是把字符串形式的ip地址转化为0xXXXXXXXX形式的地址格式。 * memcpy(&addr.S_un.S_addr,p,remoteHost->h_length); printf("ip地址为:%s\n",inet_ntoa(addr)); */ } } //此处应添加的程序如下 //程序完成后应当适当测试,需要进行的测试如下: //1.测试主机结构指针获取失败 //2.测试包含多个IP地址的主机 //3.你能想到的任何可能出现的异常情况 /////////////////结束/////////////////////////////////// system("pause");/*防止窗体关闭函数*/ return0; }
下面给大家详细介绍下上面代码的核心gethostbyname的详细用法
使用这个东西,首先要包含2个头文件:
#include<netdb.h> #include<sys/socket.h> structhostent*gethostbyname(constchar*name);
这个函数的传入值是域名或者主机名,例如"www.google.com","wpc"等等。
传出值,是一个hostent的结构(如下)。如果函数调用失败,将返回NULL。
structhostent{ char*h_name; char**h_aliases; inth_addrtype; inth_length; char**h_addr_list; };
解释一下这个结构,其中:
char*h_name表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
char**h_aliases表示的是主机的别名。www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
inth_addrtype表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是ipv6(AF_INET6)
inth_length表示的是主机ip地址的长度
int**h_addr_lisst表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。
constchar*inet_ntop(intaf,constvoid*src,char*dst,socklen_tcnt):
这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。
这个函数,其实就是返回指向dst的一个指针。如果函数调用错误,返回值是NULL。
下面是例程,有详细的注释。
#include<netdb.h> #include<sys/socket.h> intmain(intargc,char**argv) { char*ptr,**pptr; structhostent*hptr; charstr[32]; /*取得命令后第一个参数,即要解析的域名或主机名*/ ptr=argv[1]; /*调用gethostbyname()。调用结果都存在hptr中*/ if((hptr=gethostbyname(ptr))==NULL) { printf("gethostbynameerrorforhost:%s/n",ptr); return0;/*如果调用gethostbyname发生错误,返回1*/ } /*将主机的规范名打出来*/ printf("officialhostname:%s/n",hptr->h_name); /*主机可能有多个别名,将所有别名分别打出来*/ for(pptr=hptr->h_aliases;*pptr!=NULL;pptr++) printf("alias:%s/n",*pptr); /*根据地址类型,将地址打出来*/ switch(hptr->h_addrtype) { caseAF_INET: caseAF_INET6: pptr=hptr->h_addr_list; /*将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数*/ for(;*pptr!=NULL;pptr++) printf("address:%s/n",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str))); break; default: printf("unknownaddresstype/n"); break; } return0; }
另外附上获得公网与内网ip的代码:
boolgetPublicIp(string&ip) { intsock; char**pptr=NULL; structsockaddr_indestAddr; structhostent*ptr=NULL; chardestIP[128]; sock=socket(AF_INET,SOCK_STREAM,0); if(-1==sock){ perror("creatsocketfailed"); returnfalse; } bzero((void*)&destAddr,sizeof(destAddr)); destAddr.sin_family=AF_INET; destAddr.sin_port=htons(80); ptr=gethostbyname("www.ip138.com"); if(NULL==ptr){ perror("gethostbynameerror"); returnfalse; } for(pptr=ptr->h_addr_list;NULL!=*pptr;++pptr){ inet_ntop(ptr->h_addrtype,*pptr,destIP,sizeof(destIP)); printf("addr:%s\n",destIP); ip=destIP; returntrue; } returntrue; }
获取内网IP
intgetlocalip(char*outip) { #ifndefWIN32 inti=0; intsockfd; structifconfifconf; charbuf[512]; structifreq*ifreq; char*ip; //初始化ifconf ifconf.ifc_len=512; ifconf.ifc_buf=buf; strcpy(outip,"127.0.0.1"); if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) { return-1; } ioctl(sockfd,SIOCGIFCONF,&ifconf);//获取所有接口信息 close(sockfd); //接下来一个一个的获取IP地址 ifreq=(structifreq*)buf; for(i=(ifconf.ifc_len/sizeof(structifreq));i>0;i--) { ip=inet_ntoa(((structsockaddr_in*)&(ifreq->ifr_addr))->sin_addr); if(strcmp(ip,"127.0.0.1")==0)//排除127.0.0.1,继续下一个 { ifreq++; continue; } } strcpy(outip,ip); return0; #else return0; #endif }