如何发送三次 Ping 请求

本文关键字:三次 Ping 请求 何发送 | 更新日期: 2023-09-27 18:30:40

我是这个网站的新手,我是编程的初学者。每次点击发送按钮时,我都试图让该程序 ping 三次。我希望pingDetailsTextBox说类似这样的话:Pinging www.yahoo.com。 98.139.180.149 41毫秒 98.139.180.149 56毫秒 98.139.180.149 51毫秒我已经尝试了几种不同的方式来让它工作,但代码超出了我的知识范围。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Globalization;
namespace Microsoft.Samples.PingClient
{
partial class PingClientForm : Form
{
    Ping pingClient = new Ping();
    public PingClientForm()
    {
            InitializeComponent();
            pingClient.PingCompleted +=
                new PingCompletedEventHandler(pingClient_PingCompleted);

    }
    private void pingClient_PingCompleted(object sender, PingCompletedEventArgs e)
    {
        // Check to see if an error occurred.  If no error, then display 
        // the address used and the ping time in milliseconds.
        if (e.Error == null)
        {
            if (e.Cancelled)
            {
                pingDetailsTextBox.Text += "  Ping cancelled. 'r'n";
            }
            else
            {
                if (e.Reply.Status == IPStatus.Success)
                {
                    pingDetailsTextBox.Text +=
                        "  " + e.Reply.Address.ToString() + " " +
                        e.Reply.RoundtripTime.ToString(
                        NumberFormatInfo.CurrentInfo) + "ms" + "'r'n";
                }
                else
                {
                    pingDetailsTextBox.Text +=
                        "  " + GetStatusString(e.Reply.Status) + "'r'n";
                }
            }
        }
        else
        {
            // Otherwise display the error.
            pingDetailsTextBox.Text += "  Ping error.'r'n";
            MessageBox.Show(
                "An error occurred while sending this ping. " +
                e.Error.InnerException.Message);
        }
        sendButton.Enabled = true;
    }
    private string GetStatusString(IPStatus status)
    {
        switch (status)
        {
            case IPStatus.Success:
                return "Success.";
            case IPStatus.DestinationHostUnreachable:
                return "Destination host unreachable.";
            case IPStatus.DestinationNetworkUnreachable:
                return "Destination network unreachable.";
            case IPStatus.DestinationPortUnreachable:
                return "Destination port unreachable.";
            case IPStatus.DestinationProtocolUnreachable:
                return "Destination protocol unreachable.";
            case IPStatus.PacketTooBig:
                return "Packet too big.";
            case IPStatus.TtlExpired:
                return "TTL expired.";
            case IPStatus.ParameterProblem:
                return "Parameter problem.";
            case IPStatus.SourceQuench:
                return "Source quench.";
            case IPStatus.TimedOut:
                return "Timed out.";
            default:
                return "Ping failed.";
        }
    }
    private void sendButton_Click(object sender, EventArgs e)
    {
        // Select all the text in the address box.
        addressTextBox.SelectAll();
        if (addressTextBox.Text.Length != 0)
        {
            // Disable the Send button.
            sendButton.Enabled = false;
            pingDetailsTextBox.Text +=
                "Pinging " + addressTextBox.Text + " . . .'r'n";
            // Send ping request.
            pingClient.SendAsync(addressTextBox.Text, null);
        }
        else
        {
            MessageBox.Show("Please enter an IP address or host name.");
        }
    }
    private void cancelButton_Click(object sender, EventArgs e)
    {
        // Cancel any pending pings.
        pingClient.SendAsyncCancel();
    }
}

}

如何发送三次 Ping 请求

好的,我检查了文档,我认为您可以做的是进行以下编辑:

int pingCount = 0;
private void pingClient_PingCompleted(object sender, PingCompletedEventArgs e)
{
     pingCount++;
     //if error else logic etc
     if(pingCount < 3) {
        pingClient.SendAsync(addressTextBox.Text, null);
     } else {
        sendButton.Enabled = true;
     }
}
private void sendButton_Click(object sender, EventArgs e)
{
    pingCount = 0;
    //etc
}

为什么你需要做.SendAsync()PingCompleted位? 我只是.Send()

public static IPStatus CheckConn(string host, ref PingReply pngReply)
{
    Ping png = new Ping();
    try
    {
        pngReply = png.Send(host);
        return pngReply.Status;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception: " + ex.Message());
    }        
}

然后,您可以返回IPStatus并使用refpngReply带回

private void sendButton_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    btn.Enabled = false;
    string host = addressTextBox.Text;
    pingDetailsTextBox.Text = String.Empty;
    PingReply pngReply = null;
    IPStatus ipStatus;
    string strStatus = String.Empty;
    if (!String.IsNullOrEmpty(host))
    {            
        for (int i=0; i < 3; i++)
        {
            pingDetailsTextBox.Text +=
                "Pinging " + host + " . . .'r'n";
            ipStatus = CheckConn(host, ref pngReply);
            strStatus = GetStatusString(ipStatus);
            if (ipStatus == IPStatus.Success)
            {
                pingDetailsTextBox.Text +=
                    "  " + pngReply.Address.ToString() + " " +
                    pngReply.RoundtripTime.ToString(
                    NumberFormatInfo.CurrentInfo) + "ms" + "'r'n";
            }
            else
            {
                pingDetailsTextBox.Text +=
                    "  " + strStatus + "'r'n";
            }
        }
    }
    else
        MessageBox.Show("No host to ping.");
    btn.Enabled = true;
}