Usage of gethostbyname()

32 views
Skip to first unread message

Alex Vinokur

unread,
Jan 26, 2006, 8:12:12 AM1/26/06
to programming.lang.c++.sources
// ==========================
// Usage of gethostbyname()
// Language: C++
// Microsoft Windows XP Professional
// Microsoft C++ Compiler Version 13.10.3077 for 80x86
// ==========================

// --------------------
// ---- C-includes ----
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <cassert>
// --------------------

// --------------------
// --- C++-includes ---
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
// --------------------

// --------------------
// --- System calls ---
// ----- includes -----
#include <sys/types.h>
#include <winsock2.h>
// --------------------


// ====================
#define CERR cerr << "[" << __FILE__ << ", " << setw (3) << __LINE__ <<
"] ERROR: "

// ====================
enum {H_NAME, H_ALIASES, H_ADDR_LIST, SIZE_OF_HOSTBYNAME_INFO };
const char* e_names[] = {"h_name", "h_aliases", "h_addr_list" };


// ====================
string get_hostname()
{
char host_name [256];
const int ret_code = gethostname (host_name, sizeof (host_name));
if (ret_code != 0)
{
CERR << ""
<< "Unable to perform gethostname(): "
<< "ret_code = "
<< ret_code
<< ", errno = "
<< errno
<< ", "
<< "WSAGetLastError() = "
<< WSAGetLastError()
<< " - "
<< strerror (errno)
<< endl;
return string();
}
assert (strlen (host_name) < sizeof (host_name));

return string (host_name);
}


// ====================
vector<vector<string> > get_hostbyname(const string& host_name_i =
string())
{
const hostent * const p_host = gethostbyname (host_name_i.c_str());
if (p_host == 0)
{
CERR << ""
<< "Unable to perform gethostbyname("
<< host_name_i
<< "): "
<< "h_errno = "
<< h_errno
<< ", "
<< "WSAGetLastError() = "
<< WSAGetLastError()
<< " - "
<< strerror(WSAGetLastError())
;
return vector<vector<string> > ();
}

vector<vector<string> > ret_vvect (SIZE_OF_HOSTBYNAME_INFO);

ret_vvect[H_NAME].push_back (p_host->h_name);

for (int i = 0; p_host->h_aliases[i]; i++)
{
ret_vvect[H_ALIASES].push_back (p_host->h_aliases[i]);
}
for (int i = 0; p_host->h_addr_list[i]; i++)
{
ret_vvect[H_ADDR_LIST].push_back (inet_ntoa (*(struct in_addr
*)p_host->h_addr_list[i]));
}

return ret_vvect;

}

// ====================
void show_hostbyname(const string& host_name_i = string())
{
cout << endl;
cout << endl;
cout << "====== " << "gethostbyname(" << host_name_i << ")" << "
======" << endl;

const vector<vector<string> > result = get_hostbyname (host_name_i);

for (size_t i = 0; i < result.size(); i++)
{
cout << e_names[i] << endl;
if (!result[i].empty())
{
for (size_t k = 0; k < result[i].size(); k++)
{
cout << "* " << result[i][k] << endl;
}
}
else
{
cout << "! None" << endl;
}
cout << endl;
}
}

// ====================
int main ()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);

int ret_code;
const string host_name = get_hostname();
if (!host_name.empty())
{
cout << "gethostname() returns host_name = " << host_name << endl;
}
else
{
cout << "gethostname() can't return host_name" << endl;
}

// --------------------
show_hostbyname ();

show_hostbyname ("localhost");

if (!host_name.empty())
{
show_hostbyname (host_name);
}
show_hostbyname ("dummy");

WSACleanup();

return 0;
}


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Alex Vinokur

unread,
Apr 22, 2006, 3:04:10 AM4/22/06
to C++ Sources
// ==========================
// Usage of gethostbyname()
// Language: C++
// Microsoft Windows 2000 Professional
// Cygwin 1.5.18
// GNU g++ 3.4.4
// -----------------
// Version G-1-1
// -----------------
// Source: gethost.cpp
// ==========================
gethost.cpp
Reply all
Reply to author
Forward
0 new messages