Ping.SendAsync 方法

定义

尝试以异步方式将 Internet 控制消息协议 (ICMP) 回送消息发送到指定的计算机,并接收来自该计算机的相应 ICMP 回送答复消息。

重载

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

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载允许您指定操作的超时值,并控制 ICMP 数据包的碎片和生存时间值。

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

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载允许您指定操作的超时值,并控制 ICMP 回显消息数据包的碎片和生存时间值。

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

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载使您可以为操作指定一个超时值。

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

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载使您可以为操作指定一个超时值。

SendAsync(IPAddress, Int32, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。 此重载使您可以为操作指定一个超时值。

SendAsync(String, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。

SendAsync(IPAddress, Object)

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。

SendAsync(String, Int32, Object)

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。 此重载使您可以为操作指定一个超时值。

注解

这些方法不会导致应用程序的main线程阻塞。 如果要在等待 ICMP 回送答复消息时阻止,请使用 Send 方法。

注意

任何方法返回的 SendAsync IP 地址都可能源自恶意远程计算机。 请勿使用此连接到远程计算机。 使用 DNS 确定要连接到的计算机的 IP 地址。

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

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载允许您指定操作的超时值,并控制 ICMP 数据包的碎片和生存时间值。

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);
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 回送消息目标的计算机。 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。 该数组包含的字节数不能超过 65,500 个字节。

options
PingOptions

一个 PingOptions 对象,用于控制 ICMP 回显消息数据包的碎片和生存时间值。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

hostNameOrAddressnull 或是一个空字符串 ("")。

- 或 -

buffer 上声明的默认值为 null

timeout 小于零。

SendAsync 的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

hostNameOrAddress 未能解析为有效的 IP 地址。

已释放此对象。

buffer 的大小超过 65500 字节。

仅 Linux 上的 .NET 7 及更高版本:进程为非特权, buffer 不为空。

示例

下面的代码示例演示如何调用此方法。

#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 以异步方式发送 Echo 消息,当操作 (成功或不成功) 时,会将状态返回到应用程序。 SendAsync当应用程序不得阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,会引发 PingCompleted 事件。 应用程序使用 PingCompletedEventHandler 委托来指定引发 事件时 SendAsync 调用的方法。 在调用 SendAsync之前,必须将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的对象 SendAsync 。 对象 PingCompletedEventArgs 继承 UserState 属性。 此属性包含 userToken 传递到调用中的 SendAsync 对象。

如果应用程序在等待回复时应阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回显回复消息,则 ICMP 回显会失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以收到 Ping 回复。

DontFragment如果 属性为 ,true并且总数据包大小超过本地和远程计算机之间的一个路由节点可以传输的最大数据包大小,则 ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 PacketTooBig

Ttl使用 属性可以指定 ICMP 回显消息在到达目标之前可以转发的最大次数。 如果数据包在转发指定次数后未到达其目标,则会丢弃该数据包,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 TtlExpired

适用于

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

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载允许您指定操作的超时值,并控制 ICMP 回显消息数据包的碎片和生存时间值。

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);
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 回送消息目标计算机的 IPAddress

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。 该数组包含的字节数不能超过 65,500 个字节。

options
PingOptions

一个 PingOptions 对象,用于控制 ICMP 回显消息数据包的碎片和生存时间值。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

addressbuffernull

timeout 小于零。

SendAsync 的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

address 不是有效的 IP 地址。

已释放此对象。

buffer 的大小超过 65500 字节。

仅 Linux 上的 .NET 7 及更高版本:进程为非特权, buffer 不为空。

示例

下面的代码示例演示如何调用此方法。 有关回调方法的实现,请参阅 SendAsync 方法重载示例部分。

以下代码示例需要以下命名空间:

#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 回显消息。

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 以异步方式发送 Echo 消息,当操作 (成功或不成功) 时,会将状态返回到应用程序。 SendAsync当应用程序不得阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,会引发 PingCompleted 事件。 应用程序使用 PingCompletedEventHandler 委托来指定引发 事件时 SendAsync 调用的方法。 在调用 SendAsync之前,必须将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的对象 SendAsync 。 对象 PingCompletedEventArgs 继承 UserState 属性。 此属性包含 userToken 传递到调用中的 SendAsync 对象。

如果应用程序在等待回复时阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回显回复消息,则 ICMP 回显会失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以收到 Ping 回复。

DontFragment如果 属性为 ,true并且总数据包大小超过本地和远程计算机之间的一个路由节点可以传输的最大数据包大小,则 ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 PacketTooBig

Ttl使用 属性可以指定 ICMP 回显消息在到达目标之前可以转发的最大次数。 如果数据包在转发指定次数后未到达其目标,则会丢弃该数据包,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 TtlExpired

适用于

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

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到指定计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载使您可以为操作指定一个超时值。

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);
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 回送消息目标的计算机。 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。 该数组包含的字节数不能超过 65,500 个字节。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

hostNameOrAddressnull 或是一个空字符串 ("")。

- 或 -

buffer 上声明的默认值为 null

timeout 小于零。

SendAsync 的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

hostNameOrAddress 未能解析为有效的 IP 地址。

已释放此对象。

buffer 的大小超过 65500 字节。

仅 Linux 上的 .NET 7 及更高版本:进程为非特权, buffer 不为空。

示例

下面的代码示例演示如何调用 SendAsync 重载,请参阅 Ping 类概述。

注解

方法 SendAsync 以异步方式发送 Echo 消息,当操作 (成功或不成功) 时,会将状态返回到应用程序。 SendAsync当应用程序不应阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,会引发 PingCompleted 事件。 应用程序使用 PingCompletedEventHandler 委托来指定引发 事件时 SendAsync 调用的方法。 在调用 SendAsync之前,必须将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,其中包含 PingReply 描述调用结果的 SendAsync 对象。 对象 PingCompletedEventArgs 继承 UserState 属性。 此属性包含 userToken 传递到调用中的 SendAsync 对象。

如果应用程序在等待回复时应阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回显回复消息,则 ICMP 回显会失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以收到 Ping 回复。

此重载使用数据包碎片和数据包转发的默认设置。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回显消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 PacketTooBig。 如果分段) 数据包片段 (路由节点可以在丢弃之前转发 128 次。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需值。 如果数据包在转发指定次数后未到达其目标,则会丢弃该数据包,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 TtlExpired

适用于

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

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试用指定的数据缓冲区以异步方式将 Internet 控制消息协议 (ICMP) 回显消息发送到具有指定的 IPAddress 的计算机,并从该计算机接收对应的 ICMP 回显回复消息。 此重载使您可以为操作指定一个超时值。

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);
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 回送消息目标计算机的 IPAddress

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

buffer
Byte[]

一个 Byte 数组,它包含要与 ICMP 回送消息一起发送并在 ICMP 回送应答消息中返回的数据。 该数组包含的字节数不能超过 65,500 个字节。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

addressbuffernull

timeout 小于零。

SendAsync 的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

address 不是有效的 IP 地址。

已释放此对象。

buffer 的大小超过 65500 字节。

仅 Linux 上的 .NET 7 及更高版本:进程为非特权, buffer 不为空。

示例

有关演示如何调用重载的 SendAsync 代码示例,请参阅 Ping 类概述。

注解

此方法以异步方式发送回显消息,当操作 (成功或失败) 时,会将状态返回到应用程序。 SendAsync当应用程序不应阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,会引发 PingCompleted 事件。 若要指定在引发事件时SendAsync调用的方法,必须在调用 SendAsync之前将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的对象 SendAsync 。 对象 PingCompletedEventArgs 继承 UserState 属性。 此属性包含 userToken 传递到调用中的 SendAsync 对象。

注意

如果应用程序在等待回复时阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回显回复消息,则 ICMP 回显会失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以收到 Ping 回复。

此重载使用数据包碎片和数据包转发的默认设置。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回显消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 PacketTooBig。 如果分段) 数据包片段 (路由节点可以在丢弃之前转发 128 次。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需值。 如果数据包在转发指定次数后未到达其目标,则会丢弃该数据包,ICMP 回显请求将失败。 发生这种情况时, Status 将 设置为 TtlExpired

适用于

SendAsync(IPAddress, Int32, Object)

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。 此重载使您可以为操作指定一个超时值。

public:
 void SendAsync(System::Net::IPAddress ^ address, int timeout, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, int timeout, 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 回送消息目标计算机的 IPAddress

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

addressnull

timeout 小于零。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

address 不是有效的 IP 地址。

已释放此对象。

示例

有关演示如何调用重载的 SendAsync 代码示例,请参阅 Ping 类概述。

注解

方法 SendAsync 以异步方式发送 Echo 消息,当操作完成 (成功或未成功) 时,会将状态返回到应用程序。 SendAsync在应用程序不应阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,将引发 PingCompleted 事件。 应用程序使用 PingCompletedEventHandler 委托来指定引发 事件时 SendAsync 调用的方法。 在调用 SendAsync之前,必须将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的 SendAsync 对象。 对象 PingCompletedEventArgs 继承 属性 UserState 。 此属性包含 userToken 传入调用的对象 SendAsync

如果应用程序在等待答复时应阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回送回复消息,则 ICMP 回送将失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以接收 Ping 回复。

此重载使用默认设置进行数据包碎片和数据包转发。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 PacketTooBig。 数据包或数据包片段 (如果分段) 可以由路由节点转发 128 次,然后再被丢弃。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需的值。 如果数据包在被转发到指定次数后未到达其目标,则数据包将被丢弃,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 TtlExpired

适用于

SendAsync(String, Object)

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。

public:
 void SendAsync(System::String ^ hostNameOrAddress, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, 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 回送消息目标的计算机。 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

hostNameOrAddressnull 或是一个空字符串 ("")。

SendAsync(String, Object) 方法的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

hostNameOrAddress 未能解析为有效的 IP 地址。

已释放此对象。

示例

有关演示如何调用 方法的 SendAsync 代码示例,请参阅 Ping 类概述。

注解

方法 SendAsync 以异步方式发送回显消息,当操作完成 (成功或未成功) 时,会将状态返回到应用程序。 SendAsync在应用程序不应阻止时调用 方法。 每次对此方法的调用都会在从线程池自动分配的单独线程中执行。 异步操作完成后,将引发 PingCompleted 事件。 若要指定引发 事件时SendAsync调用的方法,必须在调用 SendAsync之前向 事件添加PingCompletedEventHandler委托。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的 SendAsync 对象。 对象 PingCompletedEventArgs 继承 属性 UserState 。 此属性包含 userToken 传入调用的对象 SendAsync

注意

如果应用程序在等待答复时应阻止,请使用 Send 方法;这些方法是同步的。

此方法发送包含 ICMP 回显消息的 32 Byte 数据缓冲区。 方法等待 ICMP 回送答复消息 5 秒。 如果该方法在该时间内未收到回复,则该方法将返回 , Status 并将 属性设置为 TimedOut

此重载使用默认设置进行数据包碎片和数据包转发。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 PacketTooBig

数据包或数据包片段可以由路由节点转发 128 次,然后再被丢弃。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需的值。 如果数据包在被转发到指定次数后未到达其目标,则数据包将被丢弃,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 TtlExpired

适用于

SendAsync(IPAddress, Object)

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试以异步方式向指定 IPAddress 的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。

public:
 void SendAsync(System::Net::IPAddress ^ address, System::Object ^ userToken);
public void SendAsync (System.Net.IPAddress address, 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 回送消息目标计算机的 IPAddress

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

addressnull

SendAsync 方法的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

address 不是有效的 IP 地址。

已释放此对象。

示例

有关演示如何调用 方法的 SendAsync 代码示例,请参阅 Ping 类概述。

注解

方法 SendAsync 以异步方式发送回显消息,当操作完成 (成功或未成功) 时,会将状态返回到应用程序。 SendAsync在应用程序不应阻止时调用 方法。 每次对此方法的调用都会在从线程池自动分配的单独线程中执行。 异步操作完成后,将引发 PingCompleted 事件。 若要指定引发 事件时SendAsync调用的方法,必须在调用 SendAsync之前向 事件添加PingCompletedEventHandler委托。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的 SendAsync 对象。 对象 PingCompletedEventArgs 继承 属性 UserState 。 此属性包含 userToken 传入调用的对象 SendAsync

注意

如果应用程序应在等待答复时阻止,请使用方法之 Send 一;这些方法是同步的。

此方法发送包含 ICMP 回显消息的 32 Byte 数据缓冲区。 方法等待 ICMP 回送答复消息 5 秒。 如果该时间未收到回复,则 方法返回 , Status 并将 属性设置为 TimedOut

此重载使用默认设置进行数据包碎片和数据包转发。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 PacketTooBig

数据包或数据包片段可以由路由节点转发 128 次,然后再被丢弃。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需的值。 如果数据包在被转发到指定次数后未到达其目标,则数据包将被丢弃,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 TtlExpired

适用于

SendAsync(String, Int32, Object)

Source:
Ping.cs
Source:
Ping.cs
Source:
Ping.cs

尝试以异步方式向指定的计算机发送 Internet 控制消息协议 (ICMP) 回送消息,并从该计算机接收相应的 ICMP 回送答复消息。 此重载使您可以为操作指定一个超时值。

public:
 void SendAsync(System::String ^ hostNameOrAddress, int timeout, System::Object ^ userToken);
public void SendAsync (string hostNameOrAddress, int timeout, 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 回送消息目标的计算机。 为此参数指定的值可以是主机名,也可以是以字符串形式表示的 IP 地址。

timeout
Int32

一个 Int32 值,指定(发送回送消息后)等待 ICMP 回送答复消息的最大毫秒数。

userToken
Object

一个对象,此对象将被传递给异步操作完成后所调用的方法。

例外

hostNameOrAddressnull 或是一个空字符串 ("")。

timeout 小于零。

SendAsync 的调用正在进行中。

发送或接收 ICMP 消息时引发异常。 请参见内部异常,以确切地了解所引发的异常。

hostNameOrAddress 未能解析为有效的 IP 地址。

已释放此对象。

示例

有关演示如何调用 方法的 SendAsync 代码示例,请参阅 Ping 类概述。

注解

方法 SendAsync 以异步方式发送 Echo 消息,当操作完成 (成功或未成功) 时,会将状态返回到应用程序。 SendAsync在应用程序不应阻止时调用 方法。 每个调用在从线程池自动分配的单独线程中执行。 异步操作完成后,将引发 PingCompleted 事件。 应用程序使用 PingCompletedEventHandler 委托来指定引发 事件时 SendAsync 调用的方法。 在调用 SendAsync之前,必须将委托添加到 PingCompletedEventHandler 事件。 委托的 方法接收一个 PingCompletedEventArgs 对象,该对象包含 PingReply 描述调用结果的 SendAsync 对象。 对象 PingCompletedEventArgs 继承 属性 UserState 。 此属性包含 userToken 传入调用的对象 SendAsync

如果应用程序在等待答复时应阻止,请使用 Send 方法;这些方法是同步的。

如果在 参数指定的 timeout 时间内未收到 ICMP 回送回复消息,则 ICMP 回送将失败,并将 Status 属性设置为 TimedOut

注意

当为 timeout指定非常小的数字时,即使 timeout 已过毫秒,也可以接收 Ping 回复。

此重载使用默认设置进行数据包碎片和数据包转发。 如果总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小,则包含 ICMP 回送消息的数据包可以在传输中分段。 若要防止碎片,请使用采用 参数的方法之SendAsync一,并将 属性设置为 DontFragmenttrueoptionsDontFragmenttrue 并且总数据包大小超过本地和远程计算机之间的某个路由节点可以传输的最大数据包大小时,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 PacketTooBig。 数据包或数据包片段 (如果分段) 可以由路由节点转发 128 次,然后再被丢弃。 若要更改此设置,请使用 SendAsync 采用 options 参数的重载,并将 属性设置为 Ttl 所需的值。 如果数据包在被转发到指定次数后未到达其目标,则数据包将被丢弃,ICMP 回送请求将失败。 发生这种情况时, Status 设置为 TtlExpired

适用于