char* strHost = www.baidu.com;
struct in_addr iaDestAddr; // Internet address structure
LPHOSTENT pHost; // Pointer to host entry structure
DWORD* dwAddress; // IP Address
iaDestAddr.s_addr = inet_addr(strHost);
if (INADDR_NONE == iaDestAddr.s_addr) // dealing with a name
{
pHost = gethostbyname(strHost);
}
else // dealing with an address
{
pHost = gethostbyaddr((const char*)&iaDestAddr, sizeof(struct in_addr),AF_INET);
}
if (NULL == pHost)
{
return FALSE;
}
dwAddress = (DWORD*)(*pHost->h_addr_list);

本文介绍了如何使用C语言实现从域名到IP地址的转换过程,包括通过gethostbyname和gethostbyaddr函数来获取主机信息,并对不同情况进行了区分处理。
577

被折叠的 条评论
为什么被折叠?



