c# 域名转换成ip地址

tcp协议发送到某个地址端口号是,地址是域名3322.net类型,转换成ip地址。亲测两种方法都可以。

1、参考https://blog.csdn.net/szsbell/article/details/51781220

  ///<summary>
        /// 传入域名返回对应的IP
        ///</summary>
        ///<param name="domain">域名</param>
        ///<returns></returns>
        public static string getIP(string domain)
        {
            domain = domain.Replace("http://", "").Replace("https://", "");
            IPHostEntry hostEntry = Dns.GetHostEntry(domain);
            IPEndPoint ipEndPoint = new IPEndPoint(hostEntry.AddressList[0], 0);
            return ipEndPoint.Address.ToString();

        }

2、参考https://blog.csdn.net/u013617409/article/details/77917993

IPHostEntry hostInfo = Dns.GetHostEntry(ip);
IPAddress ipAddress = hostInfo.AddressList[0];

 

posted on 2019-04-28 10:21  七七2020  阅读(2789)  评论(0编辑  收藏  举报

导航