简体   繁体   中英

Cannot ping IP address

I just found a code for ping application, it does ping ip address and domain name but when I integrate it to a parent form, it does not ping ip address but it does ping the domain name. Can anybody provide solution to this? The code is given below.

    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();
    }

if you want to ping to an adress not a domaine name you can change

IPAddress ipAddress = Dns.GetHostEntry(hostTextBox.Text).AddressList[0];

with

IPAddress ipAddress = IPAddress.Parse("youradress");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM