Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:49:31+00:00 2026-06-08T23:49:31+00:00

i am using visual c++, I want to get a domain ip address from

  • 0

i am using visual c++,

I want to get a domain ip address from domain name..
how do i get it..
i already tried gethostbyname function…
here my code…

    HOSTENT* remoteHost;        
    IN_ADDR addr;       
    hostName = "domainname.com"; 
    printf("Calling gethostbyname with %s\n", hostName);
remoteHost =gethostbyname(hostName);
memcpy(&addr.S_un.S_addr, remoteHost->h_addr, remoteHost->h_length);
printf("The IP address is: %s\n", inet_ntoa(addr));

But i get a wrong ip address.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:49:32+00:00Added an answer on June 8, 2026 at 11:49 pm

    Here’s complete source code to a little utility I find handy at times (I’ve named it “resolve”). All it does is resolve a domain name to a numeric IP (v4) address, and print it out. As-is, it’s for Windows — for Linux (or similar) you’d just need to get rid of the use_WSA class (and object thereof).

    #include <windows.h>
    #include <winsock.h>
    #include <iostream>
    #include <iterator>
    #include <exception>
    #include <algorithm>
    #include <iomanip>
    #include "infix_iterator.h"
    
    class use_WSA { 
        WSADATA d; 
        WORD ver;
    public:
        use_WSA() : ver(MAKEWORD(1,1)) { 
            if ((WSAStartup(ver, &d)!=0) || (ver != d.wVersion))
                throw(std::runtime_error("Error starting Winsock"));
        }
        ~use_WSA() { WSACleanup(); }    
    };
    
    int main(int argc, char **argv) {
        if ( argc < 2 ) {
            std::cerr << "Usage: resolve <host-name>";
            return EXIT_FAILURE;
        }
    
        try { 
            use_WSA x;
    
            hostent *h = gethostbyname(argv[1]);
            unsigned char *addr = reinterpret_cast<unsigned char *>(h->h_addr_list[0]);
            std::copy(addr, addr+4, infix_ostream_iterator<unsigned int>(std::cout, "."));
        }
        catch (std::exception const &exc) {
            std::cerr << exc.what() << "\n";
            return EXIT_FAILURE;
        }
    
        return 0;
    }
    

    This also uses the infix_ostream_iterator I’ve posted previously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to develop code using Visual Studio 2010. I just
I want to get started using Boost. I'm programming a C++ program in Visual
I am using Visual studio 2008 and I want to get the absolute path
How do you compile assembly code using Visual Studio? I want to compile and
I want to know whether Metro Applications developed using Visual Studio 11 Developer Preview
I have developed a Windows service using Visual Studio 2008. I want to install
I'm writing a standalone application in VB.NET using Visual Studio 2005. I want to
Background info: I'm using Microsoft Visual C# 2010 for this project. I want to
I am using the TeamCity Visual Studio runner. I want to add a setting
I'm using OpenGL in C++ Visual Studio 2008 Forms application and I want a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.