Click here to Skip to main content
Licence CPOL
First Posted 22 May 2005
Views 39,707
Bookmarked 21 times

CDNS 1.0 - An MFC DNS class

By | 22 May 2005 | Article
An MFC implementation of a DNS class, it can retrieve multiple IPs and hostnames.

Sample Image - CDNS.PNG

Introduction

I needed to use DNS lookups in a little Internet app I was using, and couldn't find a manageable solution. So I created one.

What are DNS lookups and why would you use one?

DNS stands for Domain Name Server/Service. It is the method which all browsers use to resolve hostnames (www.codeproject.com, for example) into IP addresses they can connect to.

How does this code work?

In my class, I have used a few Winsock functions to do the lookups, mainly GetHostByName to fill the hostent structure with information. The basic process is:

  • WSAStartup to initialize.
  • GetHostByName to fill the hostent structure.
  • Check if any information was retrieved.
  • If so, loop through the returned IPs and hostnames and add them to an array.
  • WSACleanup to end the process.

Using the class

The main three functions that will be used are:

  • SetHostname
  • DoDNSLookup
  • GetIPAt

An example of retrieving a hostname's primary IP is:

// Define a CDNS object
CDNS dnsObj;

// Set the hostname
dnsObj.SetHostname("www.google.be");

// Do the DNS lookup
BOOL doLookup = dnsObj.DoDNSLookup;

if (doLookup)
{
    // Retrieve the IP
    CString thisIP = dnsObj.GetIPAt(0);
}

Of course, the class retrieves multiple IPs, so you'll need to do a loop to get them all. Here's an example:

// Define a CDNS object
CDNS dnsObj;

// Set the hostname
dnsObj.SetHostname("www.google.be");

// Do the DNS lookup
BOOL doLookup = dnsObj.DoDNSLookup;

if (doLookup)
{
    for (int i = 0; i <= dnsObj.GetNumberOfIP(); i++)
    {
        CString thisIP = dnsObj.GetIPAt(i);
        // Do something with the IP
    }
}

History

  • 1.0
    • 22nd May, 2005: First public release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

.rich.w

CEO

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generaldns lookup PinmemberDave_Nunes7:29 16 Jan '06  
GeneralVery good Pinmemberfoobar4721:25 24 May '05  
GeneralDNS Records PinmemberAnatoly11:58 24 May '05  
GeneralRe: DNS Records PinmemberThatsAlok20:12 24 May '05  
GeneralTo those who vote 1 without reading or looking at the article or code Pinmember.rich.w10:33 22 May '05  
GeneralRe: To those who vote 1 without reading or looking at the article or code PinmemberStlan21:23 22 May '05  
GeneralRe: To those who vote 1 without reading or looking at the article or code PinmemberThatsAlok22:28 22 May '05  
GeneralRe: To those who vote 1 without reading or looking at the article or code Pinmember.rich.w4:02 23 May '05  
GeneralRe: To those who vote 1 without reading or looking at the article or code PinmemberThatsAlok17:40 23 May '05  
GeneralRe: To those who vote 1 without reading or looking at the article or code Pinmember.rich.w4:08 24 May '05  
I would expect a beginner to read the code, definately, when I was a beginner, I was always looking at code samples to try and learn from them.
 
This is besides the point though, what I am getting at is that many people just vote 1 on articles regardless of how good they are.
 
Thanks for your feedback.
GeneralRe: To those who vote 1 without reading or looking at the article or code PinmemberGeert van Horrik23:43 2 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 22 May 2005
Article Copyright 2005 by .rich.w
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid