求助

「已注销」 2009-04-21 03:13:16
我想用VC++/MFC编写一个小工具,输入域名 转换 为IP地址,谁能帮忙看下要用什么函数 相应的头文件是什么??
...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2009-04-28
  • 打赏
  • 举报
回复
还是不懂哦!!
liubuweiright 2009-04-21
  • 打赏
  • 举报
回复
学习...
gql1123 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lingyin55 的回复:]
C/C++ code
//引用

AnsiString __fastcall domainToIp(AnsiString &sTempDomain)
{
SOCKADDR_IN saddr;
WORD wVersionRequested;
WSADATA wsaData;
int nErrorNum;
wVersionRequested = MAKEWORD( 2, 2 );
nErrorNum= WSAStartup( wVersionRe…
[/Quote]

这个ok
lingyin55 2009-04-21
  • 打赏
  • 举报
回复

//引用

AnsiString __fastcall domainToIp(AnsiString &sTempDomain)
{
SOCKADDR_IN saddr;
WORD wVersionRequested;
WSADATA wsaData;
int nErrorNum;
wVersionRequested = MAKEWORD( 2, 2 );
nErrorNum= WSAStartup( wVersionRequested, &wsaData );
if ( nErrorNum!= 0 )
{
ShowMessage("error!WSAStartup failed!\n");
return "";
}
saddr.sin_family = AF_INET;

hostent *pHostIp ;

pHostIp = gethostbyname(sTempDomain.c_str());
if(pHostIp == NULL)
{
MessageBox(Application->Handle,SysErrorMessage(GetLastError()).c_str(),"初始化错",16);
return "0.0.0.0";
}
WSACleanup();
return (AnsiString)inet_ntoa(*(LPIN_ADDR)*(pHostIp->h_addr_list)) ;

}
lzh9955 2009-04-21
  • 打赏
  • 举报
回复
gethostbyname
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20040712/08/3165486.html

struct hostent FAR * gethostbyname (
const char FAR * name
);

Parameters
name
[out] A pointer to the null-terminated name of the host to resolve.
lori227 2009-04-21
  • 打赏
  • 举报
回复
This 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 who create Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.
struct hostent FAR* gethostbyname(
const char FAR* name
);
Parameters
name
[out] Pointer to the null-terminated name of the host to resolve.
Return Values
If no error occurs, this function returns a pointer to the hostent structure. If an error occurs, it returns a NULL pointer, and a specific error number can be retrieved by calling WSAGetLastError.

The following table shows the possible error codes.

Error code Description
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAHOST_NOT_FOUND An authoritative answer host was not found.
WSATRY_AGAIN A nonauthoritative host was not found, or the server failure.
WSANO_RECOVERY A nonrecoverable error occurred.
WSANO_DATA A valid name exists, but no data record of the requested type exists.
WSAEINPROGRESS A blocking Winsock 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 The socket was closed.

Note Japanese Version Winsock calls to get the machine IP address will fail if the machine name is entered in Japanese characters.
Remarks
This function returns a pointer to a hostent structure, which is 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 anything inside the structure.. 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.

This 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 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
OS Versions: Windows CE 1.0 and later.
Header: Winsock2.h.
Link Library: Ws2.lib.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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