Does the network function gethostbyname/gethostbyaddr in Linux access DNS?

Source: Internet
Author: User
Tags network function

I have been reviewing C Programming in Linux over the past two days. In addition to project requirements, I am still preparing for work. Every time I reviewed them, I had a new experience. I used to look at these two functions and thought they would be useful. I didn't have a deep understanding of their working principles. Yesterday I saw a new idea again. Since these two functions can convert IP addresses and domain names, will they access the DNS server? What should I do if I really want to access the service and the network is disconnected? With these questions, I checked them online and made a test based on the prompts. I briefly summarized them:

When gethostbyname (Domain Name> ip) and gethostbyaddr (ip> Domain Name) are resolved, the system first searches for matching information in the/etc/hosts file by default, then go to the DNS server for resolution. This order can be exchanged, as long as you modify/etc/host. the order of host (Local Machine) and bind (DNS server) in conf is enough. Let's test:

When the DNS server is configured properly, the gethostbyname parameter "www.baidu.com" can be properly resolved. If DNS is disabled, it cannot work normally. HoweverIngAdd it to the/etc/hosts file, and the function will work again. For example, we add such a group of mappings in the/etc/hosts file:

12.34.56.78 www.baidu.com

When the two functions are executed, the corresponding information of the ing is returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.