Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DNS to IP

DNS to IP

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialcomsysadmin
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Irish_GUI
    wrote on last edited by
    #1

    Hi, can someone please give me an example of how to translate a domain name eg. www.codeproject.com into its IP address. i have this working for network machine names but not for domain names. preferably into CString or unsigned long format. Thanks

    M 1 Reply Last reply
    0
    • I Irish_GUI

      Hi, can someone please give me an example of how to translate a domain name eg. www.codeproject.com into its IP address. i have this working for network machine names but not for domain names. preferably into CString or unsigned long format. Thanks

      M Offline
      M Offline
      monrobot13
      wrote on last edited by
      #2

      You could try looking at gethostbyname. - Aaron

      I 1 Reply Last reply
      0
      • M monrobot13

        You could try looking at gethostbyname. - Aaron

        I Offline
        I Offline
        Irish_GUI
        wrote on last edited by
        #3

        Aaron i cannot get that to work.. GetHostAddress("http://www.coderproject.com"); unsigned long DialogClass::GetHostAddress(LPCSTR host) { struct hostent *phe; char *p; phe = gethostbyname(host); p = *phe->h_addr_list; // Crash here return *((unsigned long*)p); } any ideas ?

        M R 2 Replies Last reply
        0
        • I Irish_GUI

          Aaron i cannot get that to work.. GetHostAddress("http://www.coderproject.com"); unsigned long DialogClass::GetHostAddress(LPCSTR host) { struct hostent *phe; char *p; phe = gethostbyname(host); p = *phe->h_addr_list; // Crash here return *((unsigned long*)p); } any ideas ?

          M Offline
          M Offline
          monrobot13
          wrote on last edited by
          #4

          Try changing:

          p = *phe->h_addr_list;

          to

          p = *phe->h_addr;

          - Aaron

          1 Reply Last reply
          0
          • I Irish_GUI

            Aaron i cannot get that to work.. GetHostAddress("http://www.coderproject.com"); unsigned long DialogClass::GetHostAddress(LPCSTR host) { struct hostent *phe; char *p; phe = gethostbyname(host); p = *phe->h_addr_list; // Crash here return *((unsigned long*)p); } any ideas ?

            R Offline
            R Offline
            RobJones
            wrote on last edited by
            #5

            Didn't compile it so it may have type-o's...

            // Name resolution
            struct hostent *hp;
            unsigned int addr;
            struct sockaddr_in name_ip;

            // Make sure that strDns is not already an IP Address.
            if(inet_addr(strDns) == INADDR_NONE)
            hp = gethostbyname(strDns);
            else
            {
            addr = inet_addr(strDns);
            hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
            }

            if(hp == NULL)
            {
            // Handle the error here..
            }

            name_ip.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
            name_ip.sin_family = AF_INET;
            name_ip.sin_port = htons(iPort);

            // Assign strSocketIP to the IP address...
            CString strSocketIP = inet_ntoa(name_ip.sin_addr);

            Rob Whoever said nothing's impossible never tried slamming a revolving door!

            I 1 Reply Last reply
            0
            • R RobJones

              Didn't compile it so it may have type-o's...

              // Name resolution
              struct hostent *hp;
              unsigned int addr;
              struct sockaddr_in name_ip;

              // Make sure that strDns is not already an IP Address.
              if(inet_addr(strDns) == INADDR_NONE)
              hp = gethostbyname(strDns);
              else
              {
              addr = inet_addr(strDns);
              hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
              }

              if(hp == NULL)
              {
              // Handle the error here..
              }

              name_ip.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
              name_ip.sin_family = AF_INET;
              name_ip.sin_port = htons(iPort);

              // Assign strSocketIP to the IP address...
              CString strSocketIP = inet_ntoa(name_ip.sin_addr);

              Rob Whoever said nothing's impossible never tried slamming a revolving door!

              I Offline
              I Offline
              Irish_GUI
              wrote on last edited by
              #6

              Rob, thanks for that but in flow of execution strDns = "www.codeproject.com"; hp = gethostbyname(strDns); executes fine name_ip.sin_addr.s_addr = *((unsigned long*)hp->h_addr); // crash occurs name_ip.sin_family = AF_INET; name_ip.sin_port = htons(iPort); any ideas?

              I 1 Reply Last reply
              0
              • I Irish_GUI

                Rob, thanks for that but in flow of execution strDns = "www.codeproject.com"; hp = gethostbyname(strDns); executes fine name_ip.sin_addr.s_addr = *((unsigned long*)hp->h_addr); // crash occurs name_ip.sin_family = AF_INET; name_ip.sin_port = htons(iPort); any ideas?

                I Offline
                I Offline
                Irish_GUI
                wrote on last edited by
                #7

                i am using winsock 1.1 if that might be a problem in the struct hostent *hp; i only have the following members: hp->h_addr_list hp->h_addrtype hp->h_aliases hp->h_length hp->h_name i do not have a hp->h_addr member

                R 2 Replies Last reply
                0
                • I Irish_GUI

                  i am using winsock 1.1 if that might be a problem in the struct hostent *hp; i only have the following members: hp->h_addr_list hp->h_addrtype hp->h_aliases hp->h_length hp->h_name i do not have a hp->h_addr member

                  R Offline
                  R Offline
                  RobJones
                  wrote on last edited by
                  #8

                  I'm not 100% sure, I have only had experence in WinSock2.. you may lookup gethostbyname in google or msdn and see if you can find any Winsock 1.1 examples.. you could snoop through the articles here too.. I'm sure it can't be too hard. If you still have problems let me konw and I'll see what I can find. Rob Whoever said nothing's impossible never tried slamming a revolving door!

                  1 Reply Last reply
                  0
                  • I Irish_GUI

                    i am using winsock 1.1 if that might be a problem in the struct hostent *hp; i only have the following members: hp->h_addr_list hp->h_addrtype hp->h_aliases hp->h_length hp->h_name i do not have a hp->h_addr member

                    R Offline
                    R Offline
                    RobJones
                    wrote on last edited by
                    #9

                    I just did a google search and found this right off the bat... The macro h_addr is defined to be h_addr_list[0] for compatibility with older software. Rob Whoever said nothing's impossible never tried slamming a revolving door!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups