根据主机名获取IP地址
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int CGetHostNameIPDlg::GetLocalIpAddress(const CString& sHostName, CString& sIPAddress)
2 {
3 struct hostent FAR *lpHostEnt = gethostbyname(sHostName);
4 if (lpHostEnt == NULL)
5 {
6 sIPAddress=_T("");
7 return WSAGetLastError();
8 }
9 LPSTR lpAddr = lpHostEnt->h_addr;//_list[0];
10 if (lpAddr)
11 {
12 struct in_addr inAddr;
13 memmove(&inAddr, lpAddr,4);
14 sIPAddress = inet_ntoa (inAddr);
15 if (sIPAddress.IsEmpty())
16 {
17 sIPAddress = _T("Not available");
18 }
19
20 }
21 return 0;
22 }
如:输入百度的主机名www.baidu.com,将返回百度的IP地址。
posted on 2011-12-23 19:11 Sunny_NUAA 阅读(631) 评论(0) 编辑 收藏 举报