首页 > 其他 > 详细

gethostbyname

时间:2015-06-09 16:14:52      阅读:288      评论:0      收藏:0      [点我收藏+]

SYNOPSIS

#include <netdb.h>

struct hostent *gethostbyname(const char *name);

Data Structure

http://www.cnblogs.com/LubinLew/p/POSIX-DataStructure.html#struct_hostent

 

Description

gethostbyname 是不可重入函数,在多线程编程时需要注意, linux中有可重入版本 gethostbyname_r,

POSIX标准里面使用getaddrinfo和getnameinfo来替换gethostbyname系列函数了,这些函数已经已经被废弃了。

 

Example

#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(void)
{
    int i = 0;
    char str[32] = {0};
     struct hostent* phost = NULL;
 
    phost = gethostbyname("www.163.com");
    if (NULL == phost)
    {
        return -1;    
    }
    
    printf("---Offical name:\n\t%s\n", phost->h_name);

    printf("---Alias name:\n");    
    for (i = 0;  phost->h_aliases[i]; i++)
    {
        printf("\t%s\n", phost->h_aliases[i]);
    }

    printf("---Address list:\n");
    for (i = 0; phost->h_addr_list[i]; i++)
    {
        printf("\t%s\n", inet_ntop(phost->h_addrtype,  phost->h_addr_list[i], str, sizeof(str)));
    }

    return 0;
} 

运行结果

---Offical name:
	163.xdwscache.glb0.lxdns.com
---Alias name:
	www.163.com
	www.163.com.lxdns.com
---Address list:
	111.202.60.48
	111.202.60.47

 

gethostbyname

原文:http://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!