PingOptions 类

定义

用于控制如何传输 Ping 数据包。

public ref class PingOptions
public class PingOptions
type PingOptions = class
Public Class PingOptions
继承
PingOptions

示例

下面的代码示例使用 PingPingOptionsPingReply 类将 ICMP 回送请求发送到命令行中指定的主机。

#using <System.dll>

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

// args[1] can be an IPaddress or host name.
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   
   Ping ^ pingSender = gcnew Ping;
   PingOptions ^ options = gcnew 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";
   array<Byte>^buffer = Encoding::ASCII->GetBytes( data );
   int timeout = 120;
   PingReply ^ reply = pingSender->Send( args[ 1 ], 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 );
   }

   
}
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

注解

PingOptions 提供 TtlDontFragment 属性,用于控制 Internet 控制消息协议 (ICMP) 回显请求数据包的传输方式。

属性 Ttl 指定 类发送 Ping 的数据包的生存时间。 此值指示在丢弃数据包之前可以转发 Ping 数据包的路由节点数。 如果要测试从源计算机向目标计算机发送数据包所需的转发数(也称为跃点),则设置此选项非常有用。

属性 DontFragment 控制发送到远程主机的数据是否可以划分为多个数据包。 如果要测试用于传输数据包的路由器和网关的最大传输单元 (MTU) ,此选项非常有用。

类的PingOptions实例传递给 SendSendAsync 方法,类PingReply通过 Options 属性返回 的PingOptions实例。

有关 实例 PingOptions的初始属性值的列表, PingOptions 请参阅 构造函数。

构造函数

PingOptions()

初始化 PingOptions 类的新实例。

PingOptions(Int32, Boolean)

初始化 PingOptions 类的新实例,并设置生存时间和分段值。

属性

DontFragment

获取或设置一个 Boolean 值,该值控制发送到远程主机的数据的分段。

Ttl

获取或设置在放弃 Ping 数据之前可以转发该数据的路由节点数。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于