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 Jan 26 2006, 8:12 am
From: "Alex Vinokur" <ale...@users.sourceforge.net>
Date: Thu, 26 Jan 2006 05:12:12 -0800
Local: Thurs, Jan 26 2006 8:12 am
Subject: Usage of gethostbyname()
// ==========================
// 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

    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