我在在通过域名解析获取IP的过程中一般使用的是DNS域名解析。
DNS协议是一种应用层协议,他是基于UDP来实现的。
2、代码实现
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
extern int h_errno;
int main(int argc, char \*\*argv)
{
char \*ptr, \*\*pptr;
char str[INET_ADDRSTRLEN];
struct hostent \*hptr;
while (--argc> 0)
{
ptr = \*++argv; // 传入域名
if ( (hptr = gethostbyname (ptr) ) == NULL) // 完成域名解析
{
printf("gethostbyname error for host: %s: %s",ptr, hstrerror (h_errno) );
continue;
}
printf ("official hostname: %s\n", hptr->h_name);
for (pptr=hptr->h_aliases; \*pptr!= NULL; pptr+