做網絡模塊的時候經常會遇到有關網址的處理.一般大致的情形是3種:
1.得到的是域名,如:www.3322.org
2.得到的是實際的IP地址,如:61.160.235.203
3.得到的是經過inet_addr處理過的IP,爲unsigned long(DWORD)
一.那麼如果是給出點分制的IP要轉爲DWORD型是如何轉化呢?這個其實最簡單,有專門的函數專門處理此事
unsigned long dwIP = inet_addr("222.212.12.77"); printf("IP(%s)->DWORD(%lu)/n"); //output IP(222.212.12.77)->DWORD(1292686558)
二.第一種情況的逆轉化
#include <stdio.h> #include <stdlib.h> #include <sys/select.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <string.h> int main(int argc, char *argv[]) { struct in_addr net; char tmp[16] = {0}; if(argc != 3) { printf("You MUST enter 3 varibal.No.1:func name No.2:case.No.3:ip(string or DWORD)/n"); return 0; } if(strcmp("1", argv[1]) == 0) { char* ip_string; ip_string = argv[2]; unsigned long dword = inet_addr(ip_string); printf("IP(%s)-->DWORD(%lu)/n", ip_string, dword); } else if(strcmp("2", argv[1]) == 0) { net.s_addr = (unsigned long)atol(argv[2]); strcpy(tmp, inet_ntoa(net)); printf("DWORD(%s)-->IP(%s)/n",argv[2], tmp); } return 0; }
這裏給出一個點分制IP和DWORD相互轉化的程序
三.如果給出的是域名而想得到點分制的IP呢?
這裏給出一個接口,支持輸入的類型是點分制和域名2中類型,返回的是DWORD型的IP
有一點要聲明的是gethostbyname這個函數必須在網絡連通的情況下才能正確完成域名的解析,你想,連個網都不通,它怎麼解析?
#include <stdio.h> #include <stdlib.h> #include <sys/select.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <string.h> #define DWORD unsigned long DWORD platform_domain2ip(const char *szDomain, char *szDotNumIP) { char szAddr[32] = {0}; struct hostent *pHost; printf("input domain name(%s)/n", szDomain); if((pHost = gethostbyname(szDomain)) == NULL) { printf("can not parse domain/n"); return -1; } printf("HostName :%s/n",pHost->h_name); strcpy(szAddr, inet_ntoa(*((struct in_addr *)pHost->h_addr))); printf("IP Address :%s/n", szAddr); strcpy(szDotNumIP, szAddr); return inet_addr(szAddr); } int main(int argc, char *argv[]) { DWORD dwip; char *ip = malloc(32); //dwip = platform_domain2ip("www.3322.org", ip); dwip = platform_domain2ip("61.160.235.203", ip); printf("ip 1 (%s) 2 dw(%lu)/n", ip, dwip); return 0; }
//可以將main的註冊分別打開來判斷下結果是否正確,這裏給出運行的結果,有圖有真相
編譯的命令再說下吧,怕有人不知道
gcc gethost.c –Wall –o gethost//在linux下
arm-hismall-linux-gcc gethost.c –Wall –o gethost//嵌入式環境下
此接口已經在我的工程中使用,在平臺IP的解析和3322的解析中得到了應用,所以是穩定可行的.
這3中IP的轉化都瞭解了的話,那麼網絡編程不就掃除了一個大石頭嗎?呵呵,大家功能進步
網上比較流行的gethostbyname的例子如下,受到了啓發
#include <stdio.h> #include <stdlib.h> #include <sys/select.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #include <string.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 1; /* 如果調用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; }