Click here to Skip to main content
Click here to Skip to main content

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

, 15 Aug 2012
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Tyronne Thomas
Software Developer (Senior)
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberTharinduMK15-Aug-12 20:16 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 15 Aug 2012
Article Copyright 2012 by Tyronne Thomas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid