Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- using namespace std;
- int main(int argc, char *argv[])
- {
- if (argc < 2)
- {
- std::cerr << "Syntaxe:\n\t" << argv[0] << " " << "adresa"
- << std::endl;
- return -1;
- }
- for(int test = 1; test < argc; test++){
- hostent *H = gethostbyname(argv[test]);
- if (H == NULL)
- {
- std::cerr << "Nepodařilo se zjistit adresu" << std::endl;
- return -1;
- }
- std::cout << "Oficiální jméno: " << H->h_name << std::endl;
- std::cout << "Alternativni jmena (aliasy): ";
- /* Budeme procházet pole H->h_aliases. Pole je ukončené NULL.*/
- char **alternativy = H->h_aliases;
- bool first = true;
- while(*alternativy != NULL)
- {
- if(first) first = false;
- else cout << ", ";
- cout << *(alternativy++);
- }
- cout << endl;
- /* Budeme procházet pole H->h_addr_list. Pole je ukončené NULL.
- Zvolíme jiný postup než v předchozím případě.*/
- std::cout << "IP adresy: " << std::endl;
- register int index = 0;
- while ( H->h_addr_list[index]!= NULL)
- {
- /* H->h_addr_list je binární forma IP adresy. Pro převod na
- tečkovou konvenci použijeme inet_ntoa.*/
- hostent *HA = gethostbyaddr(H->h_addr_list[index], 4, AF_INET);
- string name;
- if (HA == NULL)
- {
- name = "";
- } else {
- name = string(HA->h_name);
- }
- std::cout<<""<< inet_ntoa(*(in_addr *)H->h_addr_list[index])
- << " -> "
- << name
- << std::endl;
- index++;
- }
- cout << "===================================" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement