Ping.SendAsync 方法

定义

尝试以异步方式将 Internet 控制消息协议 (ICMP) 回送消息发送到指定的计算机,并接收来自该计算机的相应 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to a computer, and receive a corresponding ICMP echo reply message from that computer.

重载

SendAsync(String, Int32, Byte[], PingOptions, Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载允许您指定操作的超时值,并控制 ICMP 数据包的碎片和生存时间值。This overload allows you to specify a time-out value for the operation and control fragmentation and Time-to-Live values for the ICMP packet.

SendAsync(IPAddress, Int32, Byte[], PingOptions, Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载允许您指定操作的超时值,并控制 ICMP 回显消息数据包的碎片和生存时间值。This overload allows you to specify a time-out value for the operation and control fragmentation and Time-to-Live values for the ICMP echo message packet.

SendAsync(String, Int32, Byte[], Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

SendAsync(IPAddress, Int32, Byte[], Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

SendAsync(IPAddress, Int32, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

SendAsync(String, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer, and receive a corresponding ICMP echo reply message from that computer.

SendAsync(IPAddress, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer.

SendAsync(String, Int32, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

注解

这些方法不会导致应用程序的主线程阻塞。These methods do not cause your application's main thread to block. 如果要在等待 ICMP 回送答复消息时阻止,请使用 Send 方法。If you want to block while waiting for the ICMP echo reply message, use the Send methods.

备注

任何 SendAsync 方法返回的 IP 地址都可能源自恶意远程计算机。The IP address returned by any of the SendAsync methods can originate from a malicious remote computer. 不要使用此连接到远程计算机。Do not connect to the remote computer using this. 使用 DNS 确定要连接到的计算机的 IP 地址。Use DNS to determine the IP address of the machine to which you want to connect.

SendAsync(String, Int32, Byte[], PingOptions, Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载允许您指定操作的超时值,并控制 ICMP 数据包的碎片和生存时间值。This overload allows you to specify a time-out value for the operation and control fragmentation and Time-to-Live values for the ICMP packet.

public:
 void SendAsync(System::String ^ hostNameOrAddress, int timeout, cli::array <System::Byte> ^ buffer, System::Net::NetworkInformation::PingOptions ^ options, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, int timeout, byte[] buffer, System.Net.NetworkInformation.PingOptions options, object userToken);
member this.SendAsync : string * int * byte[] * System.Net.NetworkInformation.PingOptions * obj -> unit
Public Sub SendAsync (hostNameOrAddress As String, timeout As Integer, buffer As Byte(), options As PingOptions, userToken As Object)

参数

hostNameOrAddress
String

一个 String,它标识作为 ICMP 回送消息目标的计算机。A String that identifies the computer that is the destination for the ICMP echo message. 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。The value specified for this parameter can be a host name or a string representation of an IP address.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。A Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. 该数组包含的字节数不能超过 65,500 个字节。The array cannot contain more than 65,500 bytes.

options
PingOptions

一个 PingOptions 对象,用于控制 ICMP 回显消息数据包的碎片和生存时间值。A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

hostNameOrAddressnull 或是一个空字符串 ("")。hostNameOrAddress is null or is an empty string ("").

- 或 --or- buffernullbuffer is null.

timeout 小于零。timeout is less than zero.

SendAsync 的调用正在进行中。A call to SendAsync is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

hostNameOrAddress 未能解析为有效的 IP 地址。hostNameOrAddress could not be resolved to a valid IP address.

已释放此对象。This object has been disposed.

buffer 的大小超过 65500 字节。The size of buffer exceeds 65500 bytes.

示例

下面的代码示例演示如何调用此方法。The following code example demonstrates how to call this method.

#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::ComponentModel;
using namespace System::Threading;
void PingCompletedCallback( Object^ sender, PingCompletedEventArgs^ e );
void DisplayReply( PingReply^ reply );
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   if ( args->Length == 1 )
      throw gcnew ArgumentException( "Ping needs a host or IP Address." );

   String^ who = args[ 1 ];
   AutoResetEvent^ waiter = gcnew AutoResetEvent( false );
   
   Ping ^ pingSender = gcnew Ping;
   
   // When the PingCompleted event is raised,
   // the PingCompletedCallback method is called.
   pingSender->PingCompleted += gcnew PingCompletedEventHandler( PingCompletedCallback );
   
   // Create a buffer of 32 bytes of data to be transmitted.
   String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   array<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 = gcnew 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." );
}


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.
      (dynamic_cast<AutoResetEvent^>(e->UserState))->Set();
   }

   
   // If an error occurred, display the exception to the user.
   if ( e->Error != nullptr )
   {
      Console::WriteLine( "Ping failed:" );
      Console::WriteLine( e->Error->ToString() );
      
      // Let the main thread resume. 
      (dynamic_cast<AutoResetEvent^>(e->UserState))->Set();
   }

   PingReply ^ reply = e->Reply;
   DisplayReply( reply );
   
   // Let the main thread resume.
   (dynamic_cast<AutoResetEvent^>(e->UserState))->Set();
}


void DisplayReply( PingReply ^ reply )
{
   if ( reply == nullptr )
      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 );
   }
}

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

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the Echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不能阻塞时,调用 SendAsync 方法。Call the SendAsync method when your application must not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 应用程序使用 PingCompletedEventHandler 委托来指定 SendAsync 引发事件时调用的方法。Applications use a PingCompletedEventHandler delegate to specify the method that is called when SendAsync raises the event. 在调用 SendAsync之前,必须将 PingCompletedEventHandler 委托添加到事件。You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法;这些方法是同步的。If your application should block while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

如果 DontFragment 属性 true 并且数据包的总大小超出了本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则 ICMP 回送请求失败。If the DontFragment property is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig.

使用 Ttl 属性指定在到达其目标之前可以转发的 ICMP 回送消息的最大次数。Use the Ttl property to specify the maximum number of times the ICMP echo message can be forwarded before reaching its destination. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(IPAddress, Int32, Byte[], PingOptions, Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载允许您指定操作的超时值,并控制 ICMP 回显消息数据包的碎片和生存时间值。This overload allows you to specify a time-out value for the operation and control fragmentation and Time-to-Live values for the ICMP echo message packet.

public:
 void SendAsync(System::Net::IPAddress ^ address, int timeout, cli::array <System::Byte> ^ buffer, System::Net::NetworkInformation::PingOptions ^ options, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, int timeout, byte[] buffer, System.Net.NetworkInformation.PingOptions options, object userToken);
member this.SendAsync : System.Net.IPAddress * int * byte[] * System.Net.NetworkInformation.PingOptions * obj -> unit
Public Sub SendAsync (address As IPAddress, timeout As Integer, buffer As Byte(), options As PingOptions, userToken As Object)

参数

address
IPAddress

标识 ICMP 回送消息目标计算机的 IPAddressAn IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。A Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. 该数组包含的字节数不能超过 65,500 个字节。The array cannot contain more than 65,500 bytes.

options
PingOptions

一个 PingOptions 对象,用于控制 ICMP 回显消息数据包的碎片和生存时间值。A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

addressnulladdress is null.

- 或 --or- buffernullbuffer is null.

timeout 小于零。timeout is less than zero.

SendAsync 的调用正在进行中。A call to SendAsync is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

address 不是有效的 IP 地址。address is not a valid IP address.

已释放此对象。This object has been disposed.

buffer 的大小超过 65500 字节。The size of buffer exceeds 65500 bytes.

示例

下面的代码示例演示如何调用此方法。The following code example demonstrates how to call this method. 有关回调方法的实现,请参见 "SendAsync 方法重载示例" 部分。For an implementation of the callback method, see the SendAsync method overload example section.

下面的代码示例需要以下命名空间:The following code example requires the following namespaces:

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::Text;
using namespace System::Threading;

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;

下面的代码示例以异步方式发送 ICMP 回送消息。The following code example sends an ICMP echo message asynchronously.

void AsyncComplexLocalPing()
{
   
   // Get an object that will block the main thread.
   AutoResetEvent^ waiter = gcnew AutoResetEvent( false );
   
   // Ping's the local machine.
   Ping ^ pingSender = gcnew Ping;
   
   // When the PingCompleted event is raised,
   // the PingCompletedCallback method is called.
   pingSender->PingCompleted += gcnew PingCompletedEventHandler( PingCompletedCallback );
   IPAddress^ address = IPAddress::Loopback;
   
   // Create a buffer of 32 bytes of data to be transmitted.
   String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   array<Byte>^buffer = Encoding::ASCII->GetBytes( data );
   
   // Wait 10 seconds for a reply.
   int timeout = 10000;
   
   // 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 = gcnew PingOptions( 64,true );
   
   // Send the ping asynchronously.
   // Use the waiter as the user token.
   // When the callback completes, it can wake up this thread.
   pingSender->SendAsync( address, 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." );
}


public static void AsyncComplexLocalPing ()
{
    // Get an object that will block the main thread.
    AutoResetEvent waiter = new AutoResetEvent (false);

    // Ping's the local machine.
    Ping pingSender = new Ping ();

    // When the PingCompleted event is raised,
    // the PingCompletedCallback method is called.
    pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);

    IPAddress address = IPAddress.Loopback;

    // Create a buffer of 32 bytes of data to be transmitted.
    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    byte[] buffer = Encoding.ASCII.GetBytes (data);

    // Wait 10 seconds for a reply.
    int timeout = 10000;

    // 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);

    // Send the ping asynchronously.
    // Use the waiter as the user token.
    // When the callback completes, it can wake up this thread.
    pingSender.SendAsync (address, 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.");
}

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the Echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不能阻塞时,调用 SendAsync 方法。Call the SendAsync method when your application must not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 应用程序使用 PingCompletedEventHandler 委托来指定 SendAsync 引发事件时调用的方法。Applications use a PingCompletedEventHandler delegate to specify the method that is called when SendAsync raises the event. 在调用 SendAsync之前,必须将 PingCompletedEventHandler 委托添加到事件。You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

如果你的应用程序在等待答复时处于阻止状态,请使用 Send 方法;这些方法是同步的。If your application blocks while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

如果 DontFragment 属性 true 并且数据包的总大小超出了本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则 ICMP 回送请求失败。If the DontFragment property is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig.

使用 Ttl 属性指定在到达其目标之前可以转发的 ICMP 回送消息的最大次数。Use the Ttl property to specify the maximum number of times the ICMP echo message can be forwarded before reaching its destination. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(String, Int32, Byte[], Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

public:
 void SendAsync(System::String ^ hostNameOrAddress, int timeout, cli::array <System::Byte> ^ buffer, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, int timeout, byte[] buffer, object userToken);
member this.SendAsync : string * int * byte[] * obj -> unit
Public Sub SendAsync (hostNameOrAddress As String, timeout As Integer, buffer As Byte(), userToken As Object)

参数

hostNameOrAddress
String

一个 String,它标识作为 ICMP 回送消息目标的计算机。A String that identifies the computer that is the destination for the ICMP echo message. 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。The value specified for this parameter can be a host name or a string representation of an IP address.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。A Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. 该数组包含的字节数不能超过 65,500 个字节。The array cannot contain more than 65,500 bytes.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

hostNameOrAddressnull 或是一个空字符串 ("")。hostNameOrAddress is null or is an empty string ("").

- 或 --or- buffernullbuffer is null.

timeout 小于零。timeout is less than zero.

SendAsync 的调用正在进行中。A call to SendAsync is in progress.

hostNameOrAddress 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。hostNameOrAddress is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

hostNameOrAddress 未能解析为有效的 IP 地址。hostNameOrAddress could not be resolved to a valid IP address.

已释放此对象。This object has been disposed.

buffer 的大小超过 65500 字节。The size of buffer exceeds 65500 bytes.

示例

下面的代码示例演示如何调用 SendAsync 重载,请参见 Ping 类概述。The following code example demonstrates how to call a SendAsync overload, see the Ping class overview.

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the Echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 应用程序使用 PingCompletedEventHandler 委托来指定 SendAsync 引发事件时调用的方法。Applications use a PingCompletedEventHandler delegate to specify the method that is called when SendAsync raises the event. 在调用 SendAsync之前,必须将 PingCompletedEventHandler 委托添加到事件。You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收包含描述 SendAsync 调用结果的 PingReply 对象的 PingCompletedEventArgs 对象。The delegate's method receives a PingCompletedEventArgs object containing a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法;这些方法是同步的。If your application should block while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig. 在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments (if fragmented) can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(IPAddress, Int32, Byte[], Object)

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

public:
 void SendAsync(System::Net::IPAddress ^ address, int timeout, cli::array <System::Byte> ^ buffer, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, int timeout, byte[] buffer, object userToken);
member this.SendAsync : System.Net.IPAddress * int * byte[] * obj -> unit
Public Sub SendAsync (address As IPAddress, timeout As Integer, buffer As Byte(), userToken As Object)

参数

address
IPAddress

标识 ICMP 回送消息目标计算机的 IPAddressAn IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。A Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. 该数组包含的字节数不能超过 65,500 个字节。The array cannot contain more than 65,500 bytes.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

addressnulladdress is null.

- 或 --or- buffernullbuffer is null.

timeout 小于零。timeout is less than zero.

SendAsync 的调用正在进行中。A call to SendAsync is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

address 不是有效的 IP 地址。address is not a valid IP address.

已释放此对象。This object has been disposed.

buffer 的大小超过 65500 字节。The size of buffer exceeds 65500 bytes.

示例

有关演示调用 SendAsync 重载的代码示例,请参阅 Ping 类概述。For a code example that demonstrates calling a SendAsync overload, see the Ping class overview.

注解

此方法以异步方式发送回显消息,并在操作完成(成功或失败)后,将状态返回到应用程序。This method sends the echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 若要指定 SendAsync 引发事件时调用的方法,必须在调用 SendAsync之前将 PingCompletedEventHandler 委托添加到事件。To specify the method that is called when SendAsync raises the event, you must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

备注

如果你的应用程序在等待答复时处于阻止状态,请使用 Send 方法;这些方法是同步的。If your application blocks while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig. 在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments (if fragmented) can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(IPAddress, Int32, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

public:
 void SendAsync(System::Net::IPAddress ^ address, int timeout, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, int timeout, object userToken);
member this.SendAsync : System.Net.IPAddress * int * obj -> unit
Public Sub SendAsync (address As IPAddress, timeout As Integer, userToken As Object)

参数

address
IPAddress

标识 ICMP 回送消息目标计算机的 IPAddressAn IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

addressnulladdress is null.

timeout 小于零。timeout is less than zero.

SendAsync(IPAddress, Int32, Byte[], Object) 方法的调用正在进行中。A call to SendAsync(IPAddress, Int32, Byte[], Object) method is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

address 不是有效的 IP 地址。address is not a valid IP address.

已释放此对象。This object has been disposed.

示例

有关演示调用 SendAsync 重载的代码示例,请参阅 Ping 类概述。For a code example that demonstrates calling a SendAsync overload, see the Ping class overview.

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the Echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 应用程序使用 PingCompletedEventHandler 委托来指定 SendAsync 引发事件时调用的方法。Applications use a PingCompletedEventHandler delegate to specify the method that is called when SendAsync raises the event. 在调用 SendAsync之前,必须将 PingCompletedEventHandler 委托添加到事件。You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法;这些方法是同步的。If your application should block while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig. 在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments (if fragmented) can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(String, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer, and receive a corresponding ICMP echo reply message from that computer.

public:
 void SendAsync(System::String ^ hostNameOrAddress, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, object userToken);
member this.SendAsync : string * obj -> unit
Public Sub SendAsync (hostNameOrAddress As String, userToken As Object)

参数

hostNameOrAddress
String

一个 String,它标识作为 ICMP 回送消息目标的计算机。A String that identifies the computer that is the destination for the ICMP echo message. 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。The value specified for this parameter can be a host name or a string representation of an IP address.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

hostNameOrAddressnull 或是一个空字符串 ("")。hostNameOrAddress is null or is an empty string ("").

SendAsync(String, Object) 方法的调用正在进行中。A call to SendAsync(String, Object) method is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

hostNameOrAddress 未能解析为有效的 IP 地址。hostNameOrAddress could not be resolved to a valid IP address.

已释放此对象。This object has been disposed.

示例

有关演示调用 SendAsync 方法的代码示例,请参阅 Ping 类概述。For a code example that demonstrates calling the SendAsync method, see the Ping class overview.

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 对此方法的每个调用都在自动从线程池分配的单独线程中执行。Each call to this method executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 若要指定 SendAsync 引发事件时调用的方法,必须在调用 SendAsync之前将 PingCompletedEventHandler 委托添加到事件。To specify the method that is called when SendAsync raises the event, you must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

备注

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法;这些方法是同步的。If your application should block while waiting for a reply, use the Send methods; these methods are synchronous.

此方法使用 ICMP 回送消息发送 32 Byte 数据缓冲区。This method sends a 32 Byte data buffer with the ICMP echo message. 方法为 ICMP 回送答复消息等待5秒。The method waits five seconds for an ICMP echo reply message. 如果在此时间内未收到回复,则该方法将返回,并且 Status 属性设置为 TimedOutIf it does not receive a reply in that time the method returns and the Status property is set to TimedOut.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig.

在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(IPAddress, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer.

public:
 void SendAsync(System::Net::IPAddress ^ address, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, object userToken);
member this.SendAsync : System.Net.IPAddress * obj -> unit
Public Sub SendAsync (address As IPAddress, userToken As Object)

参数

address
IPAddress

标识 ICMP 回送消息目标计算机的 IPAddressAn IPAddress that identifies the computer that is the destination for the ICMP echo message.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

addressnulladdress is null.

SendAsync 方法的调用正在进行中。A call to the SendAsync method is in progress.

address 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

address 不是有效的 IP 地址。address is not a valid IP address.

已释放此对象。This object has been disposed.

示例

有关演示如何调用 SendAsync 方法的代码示例,请参阅 Ping 类概述。For a code example that demonstrates calling a SendAsync method, see the Ping class overview.

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 对此方法的每个调用都在自动从线程池分配的单独线程中执行。Each call to this method executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 若要指定 SendAsync 引发事件时调用的方法,必须在调用 SendAsync之前将 PingCompletedEventHandler 委托添加到事件。To specify the method that is called when SendAsync raises the event, you must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

备注

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法之一;这些方法是同步的。If your application should block while waiting for a reply, use one of the Send methods; these methods are synchronous.

此方法使用 ICMP 回送消息发送 32 Byte 数据缓冲区。This method sends a 32 Byte data buffer with the ICMP echo message. 方法为 ICMP 回送答复消息等待5秒。The method waits five seconds for an ICMP echo reply message. 如果在这段时间内没有收到回复,则该方法将返回,并且 Status 属性设置为 TimedOutIf it does not receive a reply in that time, the method returns and the Status property is set to TimedOut.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig.

在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

SendAsync(String, Int32, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer, and receive a corresponding ICMP echo reply message from that computer. 此重载使您可以为操作指定一个超时值。This overload allows you to specify a time-out value for the operation.

public:
 void SendAsync(System::String ^ hostNameOrAddress, int timeout, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, int timeout, object userToken);
member this.SendAsync : string * int * obj -> unit
Public Sub SendAsync (hostNameOrAddress As String, timeout As Integer, userToken As Object)

参数

hostNameOrAddress
String

一个 String,它标识作为 ICMP 回送消息目标的计算机。A String that identifies the computer that is the destination for the ICMP echo message. 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。The value specified for this parameter can be a host name or a string representation of an IP address.

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。An object that is passed to the method invoked when the asynchronous operation completes.

异常

hostNameOrAddressnull 或是一个空字符串 ("")。hostNameOrAddress is null or is an empty string ("").

timeout 小于零。timeout is less than zero.

SendAsync 的调用正在进行中。A call to SendAsync is in progress.

hostNameOrAddress 是一个 IPv6 地址,并且本地计算机运行的操作系统的版本低于 Windows 2000。hostNameOrAddress is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

发送或接收 ICMP 消息时引发异常。An exception was thrown while sending or receiving the ICMP messages. 请参见内部异常,以确切地了解所引发的异常。See the inner exception for the exact exception that was thrown.

hostNameOrAddress 未能解析为有效的 IP 地址。hostNameOrAddress could not be resolved to a valid IP address.

已释放此对象。This object has been disposed.

示例

有关演示调用 SendAsync 方法的代码示例,请参阅 Ping 类概述。For a code example that demonstrates calling the SendAsync method, see the Ping class overview.

注解

SendAsync 方法以异步方式发送回显消息,并在操作完成(成功或失败)时,将状态返回到应用程序。The SendAsync method sends the Echo message asynchronously and, when the operation completes (successfully or unsuccessfully), returns the status to your application. 当应用程序不应阻止时,调用 SendAsync 方法。Call the SendAsync method when your application should not block. 每个调用在自动从线程池分配的单独线程中执行。Each call executes in a separate thread that is automatically allocated from the thread pool. 异步操作完成后,它会引发 PingCompleted 事件。When the asynchronous operation completes, it raises the PingCompleted event. 应用程序使用 PingCompletedEventHandler 委托来指定 SendAsync 引发事件时调用的方法。Applications use a PingCompletedEventHandler delegate to specify the method that is called when SendAsync raises the event. 在调用 SendAsync之前,必须将 PingCompletedEventHandler 委托添加到事件。You must add a PingCompletedEventHandler delegate to the event before calling SendAsync. 委托的方法接收一个包含 PingReply 对象的 PingCompletedEventArgs 对象,该对象描述 SendAsync 调用的结果。The delegate's method receives a PingCompletedEventArgs object that contains a PingReply object that describes the result of the SendAsync call. PingCompletedEventArgs 对象继承 UserState 属性。The PingCompletedEventArgs object inherits the UserState property. 此属性包含传递到 SendAsync 调用中的 userToken 对象。This property contains the userToken object passed into the SendAsync call.

如果你的应用程序应在等待答复时阻塞,请使用 Send 方法;这些方法是同步的。If your application should block while waiting for a reply, use the Send methods; these methods are synchronous.

如果在 timeout 参数指定的时间内未收到 ICMP 回送答复消息,则 ICMP 回显将失败,并且 Status 属性将设置为 "TimedOut"。If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

备注

timeout指定非常小的数字时,即使已经过了 timeout 毫秒,也可以接收 Ping 回复。When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

此重载使用默认设置来进行数据包碎片和数据包转发。This overload uses default settings for packet fragmentation and packet forwarding. 如果数据包的总大小超出了本地和远程计算机上的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可能会分段。The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. 若要防止碎片,请使用采用 options 参数的 SendAsync 方法之一,并将 DontFragment 属性设置为 trueTo prevent fragmentation, use one of the SendAsync methods that takes an options parameter, and set the DontFragment property to true. true DontFragment 并且数据包的总大小超出了本地和远程计算机之间的路由节点可以传输的最大数据包大小时,ICMP 回送请求失败。When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. 发生这种情况时,Status 设置为 PacketTooBigWhen this happens, the Status is set to PacketTooBig. 在丢弃数据包或数据包片段之前,可以通过路由节点128次转发。The packet or packet fragments (if fragmented) can be forwarded by routing nodes 128 times before being discarded. 若要更改此设置,请使用采用 options 参数的 SendAsync 重载,并将 Ttl 属性设置为所需的值。To change this setting, use a SendAsync overload that takes an options parameter, and set the Ttl property to the desired value. 如果数据包在被转发指定的次数后无法到达其目标,则丢弃数据包,ICMP 回送请求失败。If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. 发生这种情况时,Status 设置为 TtlExpiredWhen this happens, the Status is set to TtlExpired.

安全性

NetworkInformationPermission
发送 ICMP 回送消息。To send an ICMP echo message. 关联的枚举: PingAssociated enumeration: Ping

适用于