c/c++實現獲取域名的IP地址
// GetHostIP.cpp : 定義控制臺應用程序的入口點。// #include "stdafx.h" #include <winsock2.h>#include <ws2tcpip.h>#include <stdio.h>#include <windows.h>#pragma comment(lib, "ws2_32.lib") int main(int argc, char **argv){ //----------------------------------------- // Declare and initialize variables /* * WSADATA結構被用來儲存調用AfxSocketInit全局函數返回的Windows Sockets初始化信息。 * 這個結構被用來存儲被WSAStartup函數調用后返回的Windows Sockets數據。它包含Winsock.dll執行的數據。 */ WSADATA wsaData; int iResult; DWORD dwError; /*每個word為2個字節的長度,DWORD 雙字即為4個字節,每個字節是8位*/ int i = 0; struct hostent *remoteHost; /*域名*/ char *host_name; /*主機名*/ struct in_addr addr; /*ip*/ char **pAlias; // Validate the parameters if (argc != 2) { printf("usage: GetHostIP hostname/n"); return 1; } // 此處應添加的程序如下 // 1. 當初始化winsocket iResult = WSAStartup(MAKEWORD(2,2),&wsaData);/* & 取地址*/ // 2. 檢查該socket是否初始化成功,即該socket是否等于0;如果初始化不成功,應當給出錯誤報警,并結束程序。 if(iResult!=0){ printf("初始化失敗!/n"); return 1; } /////////////////結束/////////////////////////////////// host_name = argv[1]; printf("Calling gethostbyname with %s/n", host_name); // 此處應添加的程序如下 // 1. 利用函數gethostbyname(),獲取給定主機名的指針。 remoteHost = gethostbyname(host_name); // 2. 應當熟悉該結構指針的結構 // 其中該函數申明如下:struct hostent* gethostbyname(const char *name) // 此處應添加的程序如下 // 1. 如果上面函數返回的主機結構指針為空(NULL),則做如下處理: // a. 利用函數 int WSAGetLastError ( void ) 檢查當前是否發生網絡錯誤, // b. 返回的發生的錯誤類型并作相應的處理,比如,若沒有找到主機的錯誤(此時該函數返回WSAHOST_NOT_FOUND) if(remoteHost == NULL){ //printf("gethostbynameError:%d",WSAGetLastError()); return 1; }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_addr ipAddr; * 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_addr ipAddr; * 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"); /*防止窗體關閉函數*/ return 0;}
下面給大家詳細介紹下上面代碼的核心gethostbyname的詳細用法
使用這個東西,首先要包含2個頭文件:
#include <netdb.h>#include <sys/socket.h>struct hostent *gethostbyname(const char *name);
這個函數的傳入值是域名或者主機名,例如"傳出值,是一個hostent的結構(如下)。如果函數調用失敗,將返回NULL。
struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list;};
解釋一下這個結構, 其中:
char *h_name 表示的是主機的規范名。例如www.google.com的規范名其實是www.l.google.com。
char **h_aliases 表示的是主機的別名。www.google.com就是google他自己的別名。有的時候,有的主機可能有好幾個別名,這些,其實都是為了易于用戶記憶而為自己的網站多取的名字。
int h_addrtype 表示的是主機ip地址的類型,到底是ipv4(AF_INET),還是ipv6(AF_INET6)
int h_length 表示的是主機ip地址的長度
int **h_addr_lisst 表示的是主機的ip地址,注意,這個是以網絡字節序存儲的。千萬不要直接用printf帶%s參數來打這個東西,會有問題的哇。所以到真正需要打印出這個IP的話,需要調用inet_ntop()。
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
這個函數,是將類型為af的網絡地址結構src,轉換成主機序的字符串形式,存放在長度為cnt的字符串中。
這個函數,其實就是返回指向dst的一個指針。如果函數調用錯誤,返回值是NULL。
下面是例程,有詳細的注釋。
#include <netdb.h>#include <sys/socket.h>int main(int argc, char **argv){char *ptr,**pptr;struct hostent *hptr;char str[32];/* 取得命令后第一個參數,即要解析的域名或主機名 */ptr = argv[1];/* 調用gethostbyname()。調用結果都存在hptr中 */if( (hptr = gethostbyname(ptr) ) == NULL ){printf("gethostbyname error for host:%s/n", ptr);return 0; /* 如果調用gethostbyname發生錯誤,返回1 */}/* 將主機的規范名打出來 */printf("official hostname:%s/n",hptr->h_name);/* 主機可能有多個別名,將所有別名分別打出來 */for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)printf(" alias:%s/n",*pptr);/* 根據地址類型,將地址打出來 */switch(hptr->h_addrtype){case AF_INET:case AF_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("unknown address type/n");break;}return 0;}
另外附上獲得公網與內網ip的代碼:
bool getPublicIp(string& ip){ int sock; char **pptr = NULL; struct sockaddr_in destAddr; struct hostent *ptr = NULL; char destIP[128]; sock = socket(AF_INET,SOCK_STREAM,0); if( -1 == sock ){ perror("creat socket failed"); return false; } bzero((void *)&destAddr,sizeof(destAddr)); destAddr.sin_family = AF_INET; destAddr.sin_port = htons(80); ptr = gethostbyname("www.ip138.com"); if(NULL == ptr){ perror("gethostbyname error"); return false; } 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; return true; } return true;}
獲取內網IP
int getlocalip(char* outip){#ifndef WIN32 int i=0; int sockfd; struct ifconf ifconf; char buf[512]; struct ifreq *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 = (struct ifreq*)buf; for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--) { ip = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr); if(strcmp(ip,"127.0.0.1")==0) //排除127.0.0.1,繼續下一個 { ifreq++; continue; } } strcpy(outip,ip); return 0;#else return 0;#endif }
新聞熱點
疑難解答
圖片精選