【发布时间】:2014-04-09 03:06:53
【问题描述】:
我刚刚找到了一个 ping 应用程序的代码,它可以 ping ip 地址和域名,但是当我将它集成到父表单时,它不会 ping ip 地址,但可以 ping 域名。任何人都可以提供解决方案吗?代码如下。
public FrmPing()
{
InitializeComponent();
}
private void btnPing_Click(object sender, EventArgs e)
{
try
{
int c = 3;
IPAddress ipAddress = Dns.GetHostEntry(hostTextBox.Text).AddressList[0];
resultsListView.Items .Clear();
for (int i = 0; i < c; i++)
{
System.Net.NetworkInformation.Ping ping =
new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply =
ping.Send(ipAddress);
ListViewItem result = new ListViewItem(pingReply.Address.ToString());
result.SubItems.Add(pingReply.Buffer.Count().ToString());
result.SubItems.Add(pingReply.RoundtripTime.ToString());
result.SubItems.Add(pingReply.Options.Ttl.ToString());
result.SubItems.Add(pingReply.Status.ToString());
resultsListView.Items.Add(result);
System.Threading.Thread.Sleep(100);
}
}
catch (SocketException)
{
MessageBox.Show("Could not resolve host name.");
}
catch (PingException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentNullException)
{
MessageBox.Show("Please enter the host name or IP address to ping.");
}
hostTextBox.Focus();
}
【问题讨论】:
-
您想 ping 到一个地址而不是域名?你要写地址吗?
-
@BrendanGreen 的错误消息是“无法解析主机名”。
-
@Akrem 我想同时 ping 域名和 IP 地址。在上述情况下,我无法 ping 通 IP 地址。