host name to ip - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

host name to ip

Demo Code


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

public class Main{
        public static string hostname2ip(string host)
        {
            string ip = null;

            try
            {
                ip = Dns.GetHostEntry(host).AddressList[0].ToString();
            }

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

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

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

            return ip;
        }
}

Related Tutorials