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.