PingOptions 類別

定義

用來控制如何傳送 Ping 資料封包。

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

範例

下列程式代碼範例會 Ping使用 和 PingOptionsPingReply 類別,將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 Control Message Protocol (ICMP) 回應要求封包。

屬性 Ttl 會指定 類別所傳送封包的 Ping 存留時間。 這個值表示在捨棄封包之前可以轉送 Ping 封包的路由節點數目。 如果您想要測試轉寄數目,也稱為躍點,則必須將封包從來源計算機傳送至目的地計算機,才能設定此選項。

屬性 DontFragment 可控制傳送至遠端主機的數據是否可以分割成多個封包。 如果您想要測試用來傳輸封包之路由器和閘道的最大傳輸單位 (MTU) ,此選項會很有用。

類別的PingOptions實例會傳遞至 SendSendAsync 方法,而 類別會PingReply透過 屬性傳回的PingOptionsOptions實例。

如需 實例 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)

適用於