gethost.c 987 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <errno.h>
  2. #include <netdb.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <arpa/inet.h>
  7. #include <sys/socket.h>
  8. int main(void)
  9. {
  10. in_addr_t addr = inet_addr("127.0.0.1");
  11. struct hostent *hent;
  12. hent = gethostent();
  13. if (hent == NULL) {
  14. printf("gethostent(%d):%s\n", errno, hstrerror(h_errno));
  15. exit(1);
  16. }
  17. hent = gethostbyname("localhost");
  18. if (hent == NULL) {
  19. printf("gethostbyname(%d):%s\n", errno, hstrerror(h_errno));
  20. exit(1);
  21. }
  22. hent = gethostbyname2("localhost", AF_INET);
  23. if (hent == NULL) {
  24. printf("gethostbyname2(%d):%s\n", errno, hstrerror(h_errno));
  25. exit(1);
  26. }
  27. hent = gethostbyaddr(&addr, sizeof(addr), AF_INET);
  28. if (hent == NULL) {
  29. printf("gethostbyaddr(%d):%s\n", errno, hstrerror(h_errno));
  30. exit(1);
  31. }
  32. return 0;
  33. }