Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Resolving a hostname in C# and retrieving IP version 4 address

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
15 Aug 2012CPOL 34.5K   7   1
Resolving a hostname in C#

Introduction

The code used in this article allows you to resolve a hostname in C# correctly with proper error handling and returns a IPv4 address.

Background 

There is a high probability of getting errors while resolving a host name in C#. There is no clear way to know whether the IP was resolved or not unless the Exceptions are handled correctly, Moreover when the Host is resolved you will get an IP list which can include both IP version 4 and IP version 6 addresses. The below Method is a workaround that can be used validate whether the host name was resolved correctly and then to obtain the version 4 IP address.

Using the code

Simply you can add this method to your code file and use it you will have to import the below libraries.

C#
using  System.Net;
using System.Net.Sockets;

public static bool GetResolvedConnecionIPAddress(string serverNameOrURL, 
                   out IPAddress resolvedIPAddress)
{
    bool isResolved = false;
    IPHostEntry hostEntry = null;
    IPAddress resolvIP = null;
    try
    {
        if (!IPAddress.TryParse(serverNameOrURL, out resolvIP))
        {
            hostEntry = Dns.GetHostEntry(serverNameOrURL);

            if (hostEntry != null && hostEntry.AddressList != null 
                         && hostEntry.AddressList.Length > 0)
            {
                if (hostEntry.AddressList.Length == 1)
                {
                    resolvIP = hostEntry.AddressList[0];
                    isResolved = true;
                }
                else
                {
                    foreach (IPAddress var in hostEntry.AddressList)
                    {
                        if (var.AddressFamily == AddressFamily.InterNetwork)
                        {
                            resolvIP = var;
                            isResolved = true;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            isResolved = true;
        }
    }
    catch (Exception ex)
    {
        isResolved = false;
        resolvIP=null;
    }
    finally
    {
        resolvedIPAddress = resolvIP;
    }

    return isResolved;
}

////Using the Method
IPAddress ip = null;
if (GetResolvedConnecionIPAddress("testDomain.com", out ip))
{
    ///Your Code
}

Points of Interest

This is well tested solution when it comes to resolving host name.

License

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


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
TharinduMK15-Aug-12 20:16
TharinduMK15-Aug-12 20:16 

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

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