using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class IpAddress
    {

        private static string ip = "";
        public static string getIp
        {
            get
            {
                try
                {
                    if (ip == "" || ip == null)
                    {
                        IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
                        IPAddress ipAddr = ipHost.AddressList[0];
                        ip = ipAddr.ToString();
                    }
                }
                catch (Exception ex)
                { }
                return ip;
            }
        }

        #region 单例获取值
        /*
        private IpAddress() 
        {
        }

        private static IpAddress ipAddress;
        // 定义一个标识确保线程同步
        private static readonly object locker = new object();

        public static IpAddress GetInstance()
        {
            if (ipAddress == null)
            {
                lock (locker)
                {
                    if (ipAddress == null)
                    {
                        ipAddress = new IpAddress();

                        IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
                        IPAddress ipAddr = ipHost.AddressList[0];
                        getIp = ipAddr.ToString();
                    }   
                }   
            }
            return ipAddress;
        }
        //*/
        #endregion
    }
}
