首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

只用C语言,怎么通过域名得到IP地址

2012-03-13 
只用C语言,如何通过域名得到IP地址请问各位高手,在一个单片机程序下,如何只用C语言,如何通过域名得到IP地

只用C语言,如何通过域名得到IP地址
请问各位高手,在一个单片机程序下,如何只用C语言,如何通过域名得到IP地址?
现在没有DNS服务器。

[解决办法]
gethostbyname()函数
[解决办法]
gethostbyname
The Windows Sockets gethostbyname function retrieves host information corresponding to a host name from a host database.

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

Parameters
name
[out] A pointer to the null-terminated name of the host to resolve.
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.

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 Codes
WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAHOST_NOT_FOUND Authoritative Answer Host not found.
WSATRY_AGAIN Non-Authoritative Host not found, or server failure.
WSANO_RECOVERY 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.


QuickInfo
Windows NT: Yes
Windows: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.

[解决办法]

#include "winsock2.h"
#include "iostream"
#pragma comment(lib,"ws2_32.lib")
using namespace std;


int main()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2,2),&wsadata);
hostent *phost=gethostbyname("www.hao123.com");
in_addr addr;
for(int i=0;;i++)
{
char *p=phost->h_addr_list[i];
if(p==NULL)
break;
memcpy(&addr.S_un.S_addr,p,phost->h_length);


char* ip=inet_ntoa(addr);
printf("IP地址为:%s",ip);
}

}
[解决办法]

探讨
又在网上查了一下,用AT指令 AT%DNSR 似乎可行。
明天去现场试验下。。。。

[解决办法]
都上你的当了。
[解决办法]
探讨
#include "winsock2.h"
#include "iostream"
#pragma comment(lib,"ws2_32.lib")
using namespace std;


int main()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2,2),&wsadata);
hostent *phost=gethostbyname("www.hao123.com");
in_addr addr;
for(int i=0;;i++)
{
char *p=phost->h_addr_list[i];
if(p==NULL)
break;
memcpy(&addr.S_un.S_addr,p,phost->h_length);
char* ip=inet_ntoa(addr);
printf("IP地址为:%s",ip);
}

}

热点排行