我为此进行了大量的谷歌搜索,我发现了很多例子,但没有一个对我有用。这是一个简单的问题,我觉得没有定义新类\模块等就有一个简单的答案......
我的代码是这样的:
Console.WriteLine ("Please enter an IP address or hostname");
string host = Console.ReadLine ();
***IP = resolved "host"***
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect (IP, 80);
s.close();
我如何实际解析 IP 变量?
最佳答案
您可以简单地使用 DNS类这样做:
IPHostEntry hostEntry;
hostEntry= Dns.GetHostEntry(host);
//you might get more than one ip for a hostname since
//DNS supports more than one record
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect(ip, 80);
}
关于c# - 将主机名解析为 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13248971/