本文整理汇总了C#中System.Net.NetworkInformation.Ping.SendPingAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Ping.SendPingAsync方法的具体用法?C# Ping.SendPingAsync怎么用?C# Ping.SendPingAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.NetworkInformation.Ping
的用法示例。
在下文中一共展示了Ping.SendPingAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PingAsync
/// <summary>
/// Sends the ICMP echo request to the specified IP address.
/// </summary>
/// <param name="addressIp">A destination IP address.</param>
/// <param name="timeout">Optional timeout for connection.</param>
/// <returns>ICMP echo request status.</returns>
public async Task<IPStatus> PingAsync(IPAddress addressIp, TimeSpan? timeout = null)
{
var ping = new Ping();
if (timeout.HasValue)
{
return await ping.SendPingAsync(addressIp, timeout.Value.Milliseconds)
.ContinueWith(pingTask => pingTask.Result.Status);
}
return await ping.SendPingAsync(addressIp)
.ContinueWith(pingTask => pingTask.Result.Status);
}
示例2: Button_Click
private async void Button_Click(object sender, RoutedEventArgs e)
{
// Timer a = new Timer();
//
//Ping pingSender = new Ping();
////for(int i=0;i<30;i++)
////{
// // MessageBox.Show(adr);
String adr ="172.16.16.10";
IPAddress address = IPAddress.Parse(adr);
// // pingSender.SendAsync(address,250,new PingOptions(5,true));
// pingSender.SendAsync(address, 250);
//pingSender.PingCompleted += (a, b) =>
//{
// if (b.Reply.Status == IPStatus.Success)
// {
// MessageBox.Show(b.ToString());
// }
// else
// {
// MessageBox.Show("failed");
// }
//};
//PingReply reply = await pingSender.SendPingAsync(address);
//if (reply.Status == IPStatus.Success)
//{
// ListBox1.Items.Add(adr);
//}
//else
//{
// ListBox2.Items.Add("Failed "+adr); // MessageBox.Show("falied" + adr);
//}
//}
AddressFamily d=new AddressFamily();
try
{
Ping s=new Ping();
var a= s.SendPingAsync(address).GetAwaiter();
a.OnCompleted(() => MessageBox.Show(a.GetResult().Status.ToString()));
//UdpClient udp=new UdpClient(8080);
// udp.Connect(address,8080);
// var a=new byte[]{1,2};
// await udp.SendAsync(a,44);
// TcpClient tcp = new TcpClient();
//await tcp.ConnectAsync(adr, 8080);
// MessageBox.Show(adr);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例3: PingButton_Click
async private void PingButton_Click(object sender, EventArgs e)
{
List<string> urls = new List<string>()
{
"www.habitat-spokane.org",
"www.partnersintl.org",
"www.iassist.org",
"www.fh.org",
"www.worldvision.org"
};
IPStatus status;
Func<string, Task<IPStatus>> func =
async (localUrl) =>
{
Random random = new Random();
Ping ping = new Ping();
PingReply pingReply =
await ping.SendPingAsync(localUrl);
return pingReply.Status;
};
StatusLabel.Text = "Pinging…";
foreach(string url in urls)
{
status = await func(url);
StatusLabel.Text +=
[email protected]"{ Environment.NewLine
}{ url }: { status.ToString() } ({
Thread.CurrentThread.ManagedThreadId
})";
}
}
示例4: PingButton_Click
async private void PingButton_Click(object sender, EventArgs e)
{
List<string> urls = new List<string>()
{
"www.habitat-spokane.org",
"www.partnersintl.org",
"www.iassist.org",
"www.fh.org",
"www.worldvision.org"
};
IPStatus status;
Func<string, Task<IPStatus>> func =
async (localUrl) =>
{
Ping ping = new Ping();
PingReply pingReply =
await ping.SendPingAsync(localUrl);
return pingReply.Status;
};
StatusLabel.Text = "Pinging…";
foreach(string url in urls)
{
status = await func(url);
StatusLabel.Text =
string.Format("{0}: {1} ({2})",
url, status.ToString(),
Thread.CurrentThread.ManagedThreadId);
}
}
示例5: PingButton_Click
async private void PingButton_Click(object sender, EventArgs e)
{
StatusLabel.Text = "Pinging…";
Ping ping = new Ping();
PingReply pingReply =
await ping.SendPingAsync("yahoo.com");
StatusLabel.Text = pingReply.Status.ToString();
}
示例6: ping
public async Task<long> ping(){
_latency = long.MaxValue;
try {
var pingSender = new Ping();
var reply = await pingSender.SendPingAsync(uri.Host, maxTimeout);
_latency = reply.Status == IPStatus.Success ? reply.RoundtripTime : long.MaxValue;
}
catch(PingException){ }
return _latency;
}
示例7: TryPingAsync
async Task<bool> TryPingAsync(string hostName, Ping p, ICollection<long> pings) {
try {
var reply = await p.SendPingAsync(hostName, DefaultTimeout).ConfigureAwait(false);
if ((reply != null) && (reply.Status == IPStatus.Success))
pings.Add(reply.RoundtripTime);
return true;
} catch (Exception e) {
this.Logger().FormattedWarnException(e);
return false;
}
}
示例8: Poll
public override async Task<State> Poll()
{
var ping = new Ping();
var data = "B".PadRight(32, 'B');
var buffer = Encoding.ASCII.GetBytes(data);
PingReply reply = await ping.SendPingAsync(ServerName, pingTimeout , buffer, new PingOptions(128,true));
_state = new State();
_state.Url = string.Concat(this.Name, "-", reply.Address.ToString());
_state.Status = reply.Status == IPStatus.Success ? "OK" : GetPingMessage(reply);
return _state;
}
示例9: Run_Click
private async void Run_Click(object sender, RoutedEventArgs e)
{
Ping Pings = new Ping();
int timeout = 500;
int interval = 1000;
int errors = 0;
int errorLimit = 2;
running = true;
RunButton.Visibility = Visibility.Collapsed;
StopButton.Visibility = Visibility.Visible;
StatusTextBlock.Visibility = Visibility.Visible;
while (running)
{
try {
var res = await Pings.SendPingAsync("8.8.8.8", timeout);
if (res.Status == IPStatus.Success)
{
Console("Success, time: " + res.RoundtripTime + " ms");
SetIcon("Green");
StatusTextBlock.Text = "Success";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Green;
await Task.Delay(interval - (int)res.RoundtripTime);
}
else
{
errors++;
if (errors >= errorLimit)
{
Console("Error");
SetIcon("Red");
StatusTextBlock.Text = "Error";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
}
}
}
catch
{
errors++;
if (errors >= errorLimit)
{
Console("Error");
SetIcon("Red");
StatusTextBlock.Text = "Error";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
}
await Task.Delay(interval);
}
}
SetIcon("White");
RunButton.Visibility = Visibility.Visible;
StopButton.Visibility = Visibility.Collapsed;
StatusTextBlock.Visibility = Visibility.Collapsed;
}
示例10: timer1_Tick
async private void timer1_Tick(object sender, EventArgs e)
{
Ping ping = new Ping();
try
{
await ping.SendPingAsync("github.com");
button1.Enabled = true;
}
catch (PingException)
{
button1.Enabled = false;
}
}
示例11: PingOne
public async Task<PingResult> PingOne(String address)
{
String hostname = String.Concat(address.TakeWhile(c => c != ':'));
Ping pinger = new Ping();
try
{
PingReply reply = await pinger.SendPingAsync(hostname, 1000);
return new PingResult(address, reply);
}
catch (PingException)
{
return null;
}
}
示例12: CheckInternetConnectionAsync
public static async Task<bool> CheckInternetConnectionAsync(string URL = "http://www.google.com")
{
Ping myPing = new Ping();
try
{
var pingReply = await myPing.SendPingAsync("google.com", 3000, new byte[32], new PingOptions(64, true));
if (pingReply.Status == IPStatus.Success)
{
return true;
}
}
catch
{
return false;
}
return false;
}
示例13: execute
public override async Task<WorkerResult> execute()
{
bool pingable = false;
Ping pinger = new Ping();
try
{
PingReply reply = await pinger.SendPingAsync(this.Address.Host);
pingable = reply.Status == IPStatus.Success;
return new WorkerResult() { Success = pingable, Result = reply.Buffer };
}
catch (PingException e)
{
Debug.WriteLine("Error on pinging: " + this.Address);
return new WorkerResult() { Success = false, Result = Encoding.ASCII.GetBytes(e.Message) };
}
}
示例14: GetMachineOnlineStatusAsync
/// <summary>
/// Checks machine status via ping.
/// </summary>
/// <returns>Empty string if machine is online, exception message if ping failed.</returns>
public static async Task<string> GetMachineOnlineStatusAsync(this Uri url, int timeout = 3000) {
if (url.IsFile) {
return string.Empty;
}
try {
var ping = new Ping();
var reply = await ping.SendPingAsync(url.Host, timeout);
if (reply.Status != IPStatus.Success) {
return reply.Status.ToString();
}
} catch (PingException pex) {
var pingMessage = pex.InnerException?.Message ?? pex.Message;
if (!string.IsNullOrEmpty(pingMessage)) {
return pingMessage;
}
} catch (SocketException sx) {
return sx.Message;
}
return string.Empty;
}
示例15: SendPing
public async Task<ushort> SendPing()
{
var ping = new Ping();
var reply = await ping.SendPingAsync(EndPoint.Address);
if (reply.Status != IPStatus.Success)
throw new TimeoutException();
return (ushort)reply.RoundtripTime;
}