gethostbyname()或getnameinfo()在后台如何工作?

编程入门 行业动态 更新时间:2024-10-24 10:21:29
本文介绍了gethostbyname()或getnameinfo()在后台如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

gethostbyname()或 getnameinfo()在后台如何工作?

How gethostbyname() or getnameinfo() work in background?

#include <stdlib.h> #include <stdio.h> #include <netdb.h> /* paddr: print the IP address in a standard decimal dotted format */ void paddr(unsigned char *a) { printf("%d.%d.%d.%d\n", a[0], a[1], a[2], a[3]); } main(int argc, char **argv) { struct hostent *hp; char *host = "google"; int i; hp = gethostbyname(host); if (!hp) { fprintf(stderr, "could not obtain address of %s\n", host); return 0; } for (i=0; hp->h_addr_list[i] != 0; i++) paddr((unsigned char*) hp->h_addr_list[i]); exit(0); }

google的输出:

output for google:

74.125.236.198 74.125.236.199 74.125.236.206 74.125.236.201 74.125.236.200 74.125.236.196 74.125.236.193 74.125.236.197 74.125.236.194 74.125.236.195 74.125.236.192

www.google的输出:

output for www.google:

74.125.236.210 74.125.236.209 74.125.236.212 74.125.236.208 74.125.236.211

  • 上述程序会在互联网上进行检查以解析为IP吗?
  • 为什么显示 www.google 的IP地址较少,而仅仅显示 google 的IP地址较多?
  • Will the above program do a check in the internet to resolve into IP?
  • Why its showing less IP addresses for www.google and more for just google?
  • 推荐答案

    在Linux系统上,在glibc中实现的gethostbyname()调用根据配置文件/etc/host.conf 执行查找.和/etc/nsswitch.conf .

    On a Linux system the gethostbyname() call implemented in the glibc performs lookups according to the configuration files /etc/host.conf and /etc/nsswitch.conf.

    通常,在默认配置中,如果存在给定名称的本地条目,则它将首先在/etc/hosts 文件中查找,如果存在,则返回该名称.否则,它将继续使用由/etc/resolv.conf 配置的DNS协议,其中指定了名称服务器.

    Typically in a default configuration it will first look in the /etc/hosts file if a local entry for the given name exists and if so, returns that. Otherwise it will proceed with the DNS protocol that is in turn configured by /etc/resolv.conf where the nameservers are stated.

    可以配置更多复杂的设置来查找LDAP服务器,数据库等.

    Much more complex setups can be configured that lookup LDAP servers, databases etc.

    您还可以查看一些手册页,例如 man 5 nsswitch.conf .

    You can also look into some man pages like man 5 nsswitch.conf.

    更多推荐

    gethostbyname()或getnameinfo()在后台如何工作?

    本文发布于:2023-11-27 10:13:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637653.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:后台   工作   gethostbyname   getnameinfo

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!