c# 域名转换成ip地址

时间:2022-09-27 16:22:00

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[], );
return ipEndPoint.Address.ToString(); }

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

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