Google Groups Home
Help | Sign in
Message from discussion Usage of gethostbyname()
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post will appear after it is approved by moderators
Alex Vinokur  
View profile
 More options Apr 22 2006, 3:04 am
From: Alex Vinokur <ale...@users.sourceforge.net>
Date: Sat, 22 Apr 2006 10:04:10 +0300
Local: Sat, Apr 22 2006 3:04 am
Subject: Usage of gethostbyname()

// ==========================
// 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
// ==========================

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

[ gethost.cpp ]
// ==========================
// 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
// ==========================

// --------------------
// ---- 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 <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.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
             << " - "
             << 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
           << " - "
           << hstrerror(h_errno)
           ;
      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 (*reinterpret_cast<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 ()
{
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");

    return 0;

}


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google