Ping 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
允许应用程序确定远程计算机是否可通过网络访问。
public ref class Ping : System::ComponentModel::Component
public ref class Ping : IDisposable
public ref class Ping : System::ComponentModel::Component, IDisposable
public class Ping : System.ComponentModel.Component
public class Ping : IDisposable
public class Ping : System.ComponentModel.Component, IDisposable
type Ping = class
inherit Component
type Ping = class
interface IDisposable
type Ping = class
inherit Component
interface IDisposable
Public Class Ping
Inherits Component
Public Class Ping
Implements IDisposable
Public Class Ping
Inherits Component
Implements IDisposable
- 继承
- 继承
-
Ping
- 实现
示例
下面的代码示例演示如何同步使用 Ping 类。
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
open System.Net.NetworkInformation
open System.Text
// args[0] can be an IPaddress or host name.
[<EntryPoint>]
let main args =
let pingSender = new Ping()
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
let options = PingOptions()
options.DontFragment <- true
// Create a buffer of 32 bytes of data to be transmitted.
let data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
let buffer = Encoding.ASCII.GetBytes data
let timeout = 120
let reply: PingReply = pingSender.Send(args.[0], timeout, buffer, options)
match reply.Status with
| IPStatus.Success ->
printfn "Address: %O" reply.Address
printfn "RoundTrip time: %d" reply.RoundtripTime
printfn "Time to live: %d" reply.Options.Ttl
printfn "Don't fragment: %b" reply.Options.DontFragment
printfn "Buffer size: %d" reply.Buffer.Length
0
| _ ->
eprintfn "Error sending ping: %O" reply
eprintfn "Error was: %O" reply.Status
1
下面的代码示例演示如何异步使用 Ping 类。
using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main (string[] args)
{
if (args.Length == 0)
throw new ArgumentException ("Ping needs a host or IP Address.");
string who = args[0];
AutoResetEvent waiter = new AutoResetEvent (false);
Ping pingSender = new Ping ();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
// Wait 12 seconds for a reply.
int timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(who, timeout, buffer, options, waiter);
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
private static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set ();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply (reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
public static void DisplayReply (PingReply reply)
{
if (reply == null)
return;
Console.WriteLine ("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
注解
应用程序使用该 Ping 类检测远程计算机是否可访问。
网络拓扑可以确定是否可以 Ping 成功联系远程主机。 代理、网络地址转换(NAT)设备或防火墙的存在和配置可能会阻止 Ping 成功。 成功 Ping 表示只能在网络上访问远程主机;无法保证远程主机上存在更高级别的服务(如 Web 服务器)。
此类提供的功能类似于 Ping.exe 命令行工具。 SendAsync和Send方法将 Internet 控制消息协议(ICMP)回显请求消息发送到远程计算机,并等待来自该计算机的 ICMP 回显回复消息。 有关 ICMP 消息的详细说明,请参阅以下位置 https://www.ietf.org提供的 RFC 792。
以下类型与类一起使用 Ping ,下面详细介绍了这些类型。
| 类型名称 | 说明 |
|---|---|
| IPStatus | 定义描述 ICMP 回显请求消息结果的状态代码。 |
| PingOptions | 允许你配置或检索设置,这些设置控制可以转发请求数据包的次数(Ttl以及是否可以分段)。DontFragment |
| PingReply | 包含 ICMP 回显请求的结果。 |
| PingException | 如果发生不可恢复的错误,则引发该错误。 |
| PingCompletedEventArgs | 包含与 PingCompleted 事件关联的数据,这些事件在调用完成或取消时 SendAsync 引发。 |
| PingCompletedEventHandler | 提供调用完成或取消时 SendAsync 调用的回调方法的委托。 |
和SendSendAsync方法在对象PingReply中返回答复。 该 PingReply.Status 属性返回一个 IPStatus 值来指示请求的结果。
发送请求时,必须指定远程计算机。 为此,可以提供主机名字符串、采用字符串格式的 IP 地址或对象 IPAddress 。
还可以指定以下任一类型的信息:
要随附请求的数据。 通过
buffer指定,可以了解特定大小的数据包往返远程主机和网络路径的最大传输单位所需的时间。 (请参阅采用参数的 Send 重 SendAsync 载buffer。ICMP Echo 数据包是否可以在传输中分段。 (请参阅采用 DontFragment 参数的属性和 Send 或 SendAsync 重载
options。路由节点(例如路由器或网关)可以在数据包到达目标计算机或丢弃之前转发数据包的次数。 (请参阅Ttl和SendSendAsync采用
options参数的重载。
该 Ping 类提供用于发送请求的同步和异步方法。 如果应用程序在等待答复时应阻止,请使用 Send 方法;这些方法是同步的。 如果应用程序不应阻止,请使用异步 SendAsync 方法。 调用在其 SendAsync 自己的线程中执行,该线程是从线程池自动分配的。 异步操作完成后,将引发 PingCompleted 该事件。 应用程序使用 PingCompletedEventHandler 委托来指定为 PingCompleted 事件调用的方法。 在调用SendAsync之前,必须向事件添加PingCompletedEventHandler委托。 委托的方法接收一个 PingCompletedEventArgs 对象,该 PingReply 对象包含描述调用结果 SendAsync 的对象。
不能使用相同的类实例 Ping 生成多个同时 ICMP Echo 请求。 Send在SendAsync调用正在进行或多次调用之前,SendAsync调用之前所有调用都会导致一个InvalidOperationException。
构造函数
| 名称 | 说明 |
|---|---|
| Ping() |
初始化 Ping 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| CanRaiseEvents |
获取一个值,该值指示组件是否可以引发事件。 (继承自 Component) |
| Container |
IContainer获取包含 .Component (继承自 Component) |
| DesignMode |
获取一个值,该值指示当前是否 Component 处于设计模式。 (继承自 Component) |
| Events |
获取附加到此 Component对象的事件处理程序的列表。 (继承自 Component) |
| Site | (继承自 Component) |
方法
| 名称 | 说明 |
|---|---|
| CreateObjRef(Type) |
创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。 (继承自 MarshalByRefObject) |
| Dispose() |
释放非托管资源并释放由该 Ping资源使用的托管资源。 |
| Dispose() |
释放该 Component命令使用的所有资源。 (继承自 Component) |
| Dispose(Boolean) |
释放对象使用 Ping 的非托管资源,并选择性地释放托管资源。 |
| Dispose(Boolean) |
释放由托管资源使用 Component 的非托管资源,并选择性地释放托管资源。 (继承自 Component) |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetLifetimeService() |
已过时.
检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject) |
| GetService(Type) |
返回一个对象,该对象表示服务由 Component 或其 Container提供的服务。 (继承自 Component) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| InitializeLifetimeService() |
已过时.
获取生存期服务对象来控制此实例的生存期策略。 (继承自 MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject) |
| OnPingCompleted(PingCompletedEventArgs) |
引发 PingCompleted 事件。 |
| Send(IPAddress, Int32, Byte[], PingOptions) |
尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress 数据缓冲区的计算机,并从该计算机接收相应的 ICMP 回显回复消息。 通过此重载,可为 ICMP 回显消息数据包指定操作和控制碎片和控制分段和生存时间值。 |
| Send(IPAddress, Int32, Byte[]) |
尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| Send(IPAddress, Int32) |
尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此方法允许指定操作的超时值。 |
| Send(IPAddress, TimeSpan, Byte[], PingOptions) |
尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定 IPAddress计算机,并从该计算机接收相应的 ICMP 回显回复消息。 |
| Send(IPAddress) |
尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定 IPAddress计算机,并从该计算机接收相应的 ICMP 回显回复消息。 |
| Send(String, Int32, Byte[], PingOptions) |
尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许为 ICMP 数据包指定操作的超时值,并控制碎片和生存时间值。 |
| Send(String, Int32, Byte[]) |
尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| Send(String, Int32) |
尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此方法允许指定操作的超时值。 |
| Send(String, TimeSpan, Byte[], PingOptions) |
尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 |
| Send(String) |
尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 |
| SendAsync(IPAddress, Int32, Byte[], Object) |
异步尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| SendAsync(IPAddress, Int32, Byte[], PingOptions, Object) |
异步尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并从该计算机接收相应的 ICMP 回显回复消息。 通过此重载,可为 ICMP 回显消息数据包指定操作和控制碎片和控制分段和生存时间值。 |
| SendAsync(IPAddress, Int32, Object) |
异步尝试将 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress计算机的回显消息,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| SendAsync(IPAddress, Object) |
异步尝试将 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress计算机的回显消息,并从该计算机接收相应的 ICMP 回显回复消息。 |
| SendAsync(String, Int32, Byte[], Object) |
异步尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| SendAsync(String, Int32, Byte[], PingOptions, Object) |
异步尝试将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许为 ICMP 数据包指定操作的超时值,并控制碎片和生存时间值。 |
| SendAsync(String, Int32, Object) |
异步尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 此重载允许指定操作的超时值。 |
| SendAsync(String, Object) |
异步尝试将 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并从该计算机接收相应的 ICMP 回显回复消息。 |
| SendAsyncCancel() |
取消所有挂起的异步请求以发送 Internet 控制消息协议(ICMP)回显消息并接收相应的 ICMP 回显回复消息。 |
| SendPingAsync(IPAddress, Int32, Byte[], PingOptions) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 此重载允许你为操作指定超时值、用于发送和接收的缓冲区,以及控制 ICMP 回显消息数据包的碎片和生存时间值。 |
| SendPingAsync(IPAddress, Int32, Byte[]) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值,以及用于发送和接收的缓冲区。 |
| SendPingAsync(IPAddress, Int32) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值。 |
| SendPingAsync(IPAddress, TimeSpan, Byte[], PingOptions, CancellationToken) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值、用于发送和接收的缓冲区、控制碎片和生存时间值,以及 CancellationToken ICMP 回显消息数据包的缓冲区。 |
| SendPingAsync(IPAddress) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到具有指定 IPAddress数据缓冲区的计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 |
| SendPingAsync(String, Int32, Byte[], PingOptions) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并将相应 ICMP 回显回复消息作为异步操作从该计算机接收。 此重载允许你为操作指定超时值、用于发送和接收的缓冲区,以及控制 ICMP 回显消息数据包的碎片和生存时间值。 |
| SendPingAsync(String, Int32, Byte[]) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并将相应 ICMP 回显回复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值,以及用于发送和接收的缓冲区。 |
| SendPingAsync(String, Int32) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并将相应 ICMP 回显回复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值。 |
| SendPingAsync(String, TimeSpan, Byte[], PingOptions, CancellationToken) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并将相应 ICMP 回显答复消息作为异步操作从该计算机接收。 此重载允许指定操作的超时值、用于发送和接收的缓冲区、控制碎片和生存时间值,以及 CancellationToken ICMP 回显消息数据包的缓冲区。 |
| SendPingAsync(String) |
将具有指定数据缓冲区的 Internet 控制消息协议(ICMP)回显消息发送到指定计算机,并将相应 ICMP 回显回复消息作为异步操作从该计算机接收。 |
| ToString() |
返回包含 String 的名称 Component(如果有)。 不应重写此方法。 (继承自 Component) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
活动
| 名称 | 说明 |
|---|---|
| Disposed |
当组件通过对方法的调用 Dispose() 释放时发生。 (继承自 Component) |
| PingCompleted |
当异步操作发送 Internet 控制消息协议(ICMP)回显消息并接收相应的 ICMP 回显答复消息完成或取消时发生。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
释放类实例 Ping 使用的所有资源。 |