Source:GetHostByNameC++

From Codemotion
Jump to: navigation, search

Source:GetHostByNameC++

Description Simple Host name --> Ip Conversion in C++
Author(s) Root
Version N/A
Language(s) C++
Compiler DevC++ (GCC)
Latest Release 06-16-09
OS Windows XP/Vista
Discussion Thread [No thread in Forums]
GetHostByNameC++

Description

Simple console program to convert host names to Ip's. For example "codemotion.net" will give 87.98.139.1. You must add "libws2_32.a" in linker for this to work.

  1. #include <winsock.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7.  
  8. {
  9.     struct in_addr address;
  10.     WSADATA Data;
  11.     int result;
  12.     result = WSAStartup(MAKEWORD(2, 2), &Data);
  13.  
  14.     if (result != 0){
  15.         cout << "WSAStartup fail: %d\n" << result;
  16.         return 1;
  17.     }
  18.  
  19.     for(;;){
  20.     cout << "~Please enter domain name." << endl <<endl;
  21.     char host_name[32];
  22.     cin >> host_name;
  23.     cout << endl;
  24.     cout << "Determining IP from DNS..." <<endl << endl;
  25.     struct hostent* address2 = gethostbyname(host_name);
  26.     if (address2 != 0) {
  27.     address.s_addr = *(u_long *) address2->h_addr_list[0];
  28.     cout << inet_ntoa(address) << endl <<endl << endl;
  29.     }
  30.  
  31.     else{              
  32.     cout << "Domain doesn't exist." << endl << endl << endl;
  33.     Sleep(2000);
  34.     }
  35. }
  36. }

See Also


Personal tools