Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. $ cat ex.c
  2. #include <arpa/inet.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/socket.h>
  8.  
  9. int main(int argc, char *argv[]) {
  10. if (argc < 2) {
  11. printf("Use as %s hostname", argv[0]);
  12. return EXIT_FAILURE;
  13. }
  14.  
  15. struct hostent *hp = gethostbyname(argv[1]);
  16.  
  17. if (hp == NULL) {
  18. printf("gethostbyname() failed\n");
  19. } else {
  20. printf("%s = ", hp->h_name);
  21. int i = 0;
  22. while (hp->h_addr_list[i] != NULL) {
  23. printf("%s ", inet_ntoa(*(struct in_addr *)(hp->h_addr_list[i])));
  24. i++;
  25. }
  26. printf("\n");
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement