gethostbyname代码示例(Windows CE)

 1 /* 
 2  * 功能: 由域名得到 IP 地址 
 3  * 入口参数: pDomain 域名,如: www.163.com 
 4  *           pIPBuff 保存解析后的 IP 
 5  * 出口参数: pIPBuff 保存解析后的 IP 
 6  * 返回值: TRUE:成功;FALSE:失败 
 7 */  
 8 BOOL CGetIPFromURLDlg::DomainToIP(char *pDomain,char *pIPBuff)      
 9 {  
10     unsigned long lgIP = inet_addr(pDomain);  
11   
12     WSADATA wsaData;  
13     WSAStartup(MAKEWORD(1,1),&wsaData);  
14   
15     //输入的IP字符串  
16     if(lgIP != INADDR_NONE)  
17     {  
18         memcpy(pIPBuff,pDomain,strlen(pDomain));  
19         WSACleanup();  
20   
21         return TRUE;  
22     }  
23   
24     HOSTENT *host_entry;  
25     host_entry = gethostbyname(pDomain);  
26   
27     if(host_entry != 0)       
28     {  
29         sprintf(pIPBuff,"%d.%d.%d.%d",  
30             (host_entry->h_addr_list[0][0] & 0xff),  
31             (host_entry->h_addr_list[0][1] & 0xff),  
32             (host_entry->h_addr_list[0][2] & 0xff),  
33             (host_entry->h_addr_list[0][3] & 0xff));  
34     }  
35     else  
36     {  
37         WSACleanup();  
38         return FALSE;     
39     }   
40   
41     WSACleanup();  
42     return TRUE;   
43 }  

 

posted @ 2016-03-05 08:45  91program  阅读(609)  评论(0编辑  收藏  举报