Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Usage of gethostbyname()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
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.
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 3K ]
// ==========================
// 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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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