Resolves an IP address to its hostname - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

Resolves an IP address to its hostname

Demo Code


using System.Text;
using System.Net.NetworkInformation;
using System.Net;
using System;/*  w  w w.j a va  2  s  .c o  m*/

public class Main{
        /// <summary>
        /// Resolves an IP address to its hostname
        /// </summary>
        /// <param name="ip"> The host's IP address (string)</param>
        /// <returns> The hostname (string)</returns>
        /// <exception cref="System.Net.Sockets.SocketException"></exception>
        public static string IP2Hostname(string ip)
        {
            string host = null;

            try
            {
                host = Dns.GetHostEntry(ip).HostName;
            }

            catch (System.Net.Sockets.SocketException sEx)
            {
                host = sEx.Message;
            }

            catch (ArgumentException aEx)
            {
                host = aEx.Message;
            }

            catch (Exception e)
            {
                host = e.Message;
            }

            return host;
        }
}

Related Tutorials