Export (0) Print
Expand All

PingOptions Class

Used to control how Ping data packets are transmitted.

System.Object
  System.Net.NetworkInformation.PingOptions

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

public class PingOptions

The PingOptions type exposes the following members.

  NameDescription
Public methodPingOptions()Initializes a new instance of the PingOptions class.
Public methodPingOptions(Int32, Boolean)Initializes a new instance of the PingOptions class and sets the Time to Live and fragmentation values.
Top

  NameDescription
Public propertyDontFragmentGets or sets a Boolean value that controls fragmentation of the data sent to the remote host.
Public propertyTtlGets or sets the number of routing nodes that can forward the Ping data before it is discarded.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

The Ttl 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 DontFragment 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 Options 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);
            }
        }
    }
}

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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