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] | |||
| ||||
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.
#include <winsock.h>#include <iostream>using namespace std;
int main()
{struct in_addr address;
WSADATA Data;int result;
result = WSAStartup(MAKEWORD(2, 2), &Data);
if (result != 0){
cout << "WSAStartup fail: %d\n" << result;
return 1;
}for(;;){
cout << "~Please enter domain name." << endl <<endl;
char host_name[32];
cin >> host_name;
cout << endl;
cout << "Determining IP from DNS..." <<endl << endl;
struct hostent* address2 = gethostbyname(host_name);
if (address2 != 0) {
address.s_addr = *(u_long *) address2->h_addr_list[0];
cout << inet_ntoa(address) << endl <<endl << endl;
}else{
cout << "Domain doesn't exist." << endl << endl << endl;
Sleep(2000);
}}}