我是这个网站的新手,我是编程的初学者。每次按下发送按钮时,我都试图让这个程序ping三次。我希望pingDetailsTextBox能够说出类似的内容:Ping www.yahoo.com。 。 。 98.139.180.149 41ms 98.139.180.149 56ms 98.139.180.149 51ms 我已经尝试了几种不同的东西来使它工作,但代码是我所不知道的。
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();
}
}
}
答案 0 :(得分:0)
好的,我检查了文档,我认为您可以做的是进行以下编辑:
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
}
答案 1 :(得分:0)
为什么需要.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
并使用ref
即可将此pngReply
退出:
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;
}