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];

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM