Linux Get host information

Source: Internet
Author: User


The information of the host in Linux is usually stored in/etc/hosts, and we can read the file information through the function gethostent.




Note that the address of the struct Hostent struct object returned is a static buffer, the address of this static buffer is fixed, and when Gethostent is called multiple times, the
Will overwrite the contents of the front buffer, and the contents of the buffer pointed to by the multiple reads are consistent, which helps to preventThere is a memory leak caused by releasing memory.
  
 
  1. #include<netdb.h>
  2. #include<stdio.h>
  3. void getHostent()
  4. {
  5. struct hostent* host1;
  6. host1 = gethostent();
  7. struct hostent* host2;
  8. host2 = gethostent();
  9. printf("host1:%x,host2:%x\n",host1,host2);
  10. printf("h_name:%s\n",host1->h_name);
  11. ///////
  12. }
  13. int main()
  14. {
  15. getHostent();
  16. return 0;
  17. }



If the Hosts file is not open when calling Gethostent, the function opens the file, and if you want to close the file, you can use the Endhostent function:







From for notes (Wiz)

Linux Get host information

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.