做了一个ping程序,但发现Dns.GetHostEntry函数不能解析ip,只能解析域名。
运行结果如下:
ip为上面的百度ip
代码段如下:
string Hostclient = textBox1.Text;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.ReceiveTimeout = 1000;
IPHostEntry Hostinfo;
try
{
Hostinfo = Dns.GetHostEntry(Hostclient);
}
catch(Exception )
{
listBox1.Items.Add("无法辨识主机!");
return;
}