Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <netdb.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     if (argc < 2)
  11.     {
  12.         std::cerr << "Syntaxe:\n\t" << argv[0] << " " << "adresa"
  13.          << std::endl;
  14.         return -1;
  15.     }
  16.   for(int test = 1; test < argc; test++){
  17.     hostent *H = gethostbyname(argv[test]);
  18.     if (H == NULL)
  19.     {
  20.         std::cerr << "Nepodařilo se zjistit adresu" << std::endl;
  21.             return -1;
  22.     }
  23.     std::cout << "Oficiální jméno: " << H->h_name << std::endl;
  24.     std::cout << "Alternativni jmena (aliasy): ";
  25.     /* Budeme procházet pole H->h_aliases. Pole je ukončené NULL.*/
  26.           char **alternativy = H->h_aliases;
  27.     bool first = true;
  28.     while(*alternativy != NULL)
  29.     {
  30.       if(first) first = false;
  31.       else cout << ", ";
  32.         cout << *(alternativy++);
  33.     }
  34.     cout << endl;
  35.     /* Budeme procházet pole H->h_addr_list. Pole je ukončené NULL.
  36.              Zvolíme jiný postup než v předchozím případě.*/
  37.     std::cout << "IP adresy: " << std::endl;
  38.     register int index = 0;
  39.     while ( H->h_addr_list[index]!= NULL)
  40.     {
  41.         /* H->h_addr_list je binární forma IP adresy. Pro převod na
  42.            tečkovou konvenci použijeme inet_ntoa.*/
  43.         hostent *HA = gethostbyaddr(H->h_addr_list[index], 4, AF_INET);
  44.         string name;
  45.         if (HA == NULL)
  46.         {
  47.               name = "";
  48.         } else {
  49.             name = string(HA->h_name);
  50.         }
  51.         std::cout<<""<< inet_ntoa(*(in_addr *)H->h_addr_list[index])
  52.          << " -> "
  53.          << name
  54.          << std::endl;
  55.          index++;
  56.     }
  57.     cout << "===================================" << endl;
  58.   }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement