PingOptions Class
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Used to control how Ping data packets are transmitted.

Namespace:   System.Net.NetworkInformation
Assembly:  System (in System.dll)

SystemObject
  System.Net.NetworkInformationPingOptions

public class PingOptions
public ref class PingOptions 
type PingOptions = class end
Public Class PingOptions
NameDescription
System_CAPS_pubmethod PingOptions

Initializes a new instance of the PingOptions class.

System_CAPS_pubmethod PingOptions

Initializes a new instance of the PingOptions class and sets the Time to Live and fragmentation values.

NameDescription
System_CAPS_pubproperty

Gets or sets a Boolean value that controls fragmentation of the data sent to the remote host.

System_CAPS_pubproperty

Gets or sets the number of routing nodes that can forward the Ping data before it is discarded.

NameDescription
System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

The PingOptions class provides the and properties to control how Internet Control Message Protocol (ICMP) echo request packets are transmitted.

The property specifies the Time to Live for packets sent by the Ping class. This value indicates the number of routing nodes that can forward a Ping packet before it is discarded. Setting this option is useful if you want to test the number of forwards, also known as hops, are required to send a packet from a source computer to a destination computer.

The property controls whether data sent to a remote host can be divided into multiple packets. This option is useful if you want to test the maximum transmission unit (MTU) of the routers and gateways used to transmit the packet.

Instances of the PingOptions class are passed to the Send and SendAsync methods, and the PingReply class returns instances of PingOptions via the property.

For a list of initial property values for an instance of PingOptions, see the PingOptions constructor.

The following code example uses the Ping, PingOptions and PingReply classes to send an ICMP echo request to the host specified on the command line.

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


}
.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top