域名->IP

「已注销」 2009-05-04 08:06:14
我想用VC++/MFC编写一个小工具,输入域名 转换 为IP地址,谁能帮忙看下要用什么函数 相应的头文件是什么?? 具体点,我已经做好了图形界面了。。。
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
caitian6 2009-05-05
  • 打赏
  • 举报
回复
顶楼上
huliang66 2009-05-05
  • 打赏
  • 举报
回复

#include<winsock2.h>
#pragma comment(lib, "Ws2_32.lib")

WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
sprintf(name,"%s","http://www.sohu.com");
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if((hostinfo = gethostbyname("http://www.sohu.com")) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
WSACleanup( );
}
MessageBox(ip);



hzcenter 2009-05-05
  • 打赏
  • 举报
回复
dns不错
greatws 2009-05-05
  • 打赏
  • 举报
回复
DnsQuery_A (for ANSI encoding)

DnsQuery_W (for Unicode encoding)

DnsQuery_UTF8 (for UTF-8 encoding)

也可以用如上3个函数获得
Wenxy1 2009-05-04
  • 打赏
  • 举报
回复
MSDN:

gethostbyname
The gethostbyname function retrieves host information corresponding to a host name from a host database.

Note The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

struct hostent FAR *gethostbyname(
const char FAR *name
);
Parameters
name
[in] Pointer to the null-terminated name of the host to resolve.
Return Values
If no error occurs, gethostbyname returns a pointer to the hostent structure described above. Otherwise, it returns a NULL pointer and a specific error number can be retrieved by calling WSAGetLastError.

Error code Meaning
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAHOST_NOT_FOUND Authoritative answer host not found.
WSATRY_AGAIN Nonauthoritative host not found, or server failure.
WSANO_RECOVERY A nonrecoverable error occurred.
WSANO_DATA Valid name, no data record of requested type.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEFAULT The name parameter is not a valid part of the user address space.
WSAEINTR A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.


Remarks
The gethostbyname function returns a pointer to a hostent structure—a structure allocated by Windows Sockets. The hostent structure contains the results of a successful search for the host specified in the name parameter.

The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

The gethostbyname function cannot resolve IP address strings passed to it. Such a request is treated exactly as if an unknown host name were passed. Use inet_addr to convert an IP address string the string to an actual IP address, then use another function, gethostbyaddr, to obtain the contents of the hostent structure.

The gethostbyname function resolves the string returned by a successful call to gethostname.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧