Export (0) Print
Expand All

Ping.Send Method (String)

Attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer, and receive a corresponding ICMP echo reply message from that computer.

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

public PingReply Send(
	string hostNameOrAddress
)

Parameters

hostNameOrAddress
Type: System.String

A String that identifies the computer that is the destination for the ICMP echo message. The value specified for this parameter can be a host name or a string representation of an IP address.

Return Value

Type: System.Net.NetworkInformation.PingReply
A PingReply object that provides information about the ICMP echo reply message, if one was received, or provides the reason for the failure, if no message was received.

ExceptionCondition
ArgumentNullException

hostNameOrAddress is null or is an empty string ("").

InvalidOperationException

A call to SendAsync is in progress.

NotSupportedException

address is an IPv6 address and the local computer is running an operating system earlier than Windows 2000.

PingException

An exception was thrown while sending or receiving the ICMP messages. See the inner exception for the exact exception that was thrown.

ObjectDisposedException

This object has been disposed.

This method sends a 32 Byte data buffer with the ICMP echo message. The method waits five seconds for an ICMP echo reply message. If it does not receive a reply in that time, the method returns and the Status property is set to TimedOut.

This overload uses default settings for packet fragmentation and packet forwarding. The packet that contains the ICMP echo message can be fragmented in transit if the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers. To prevent fragmentation, use one of the Send methods that takes an options parameter, and set the DontFragment property to true. When DontFragment is true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing nodes between the local and remote computers, the ICMP echo request fails. When this happens, the Status is set to PacketTooBig. The packet or packet fragments (if fragmented) can be forwarded by routing nodes 128 times before being discarded. To change this setting, use a Send overload that takes an options parameter, and set the Ttl property to the desired value. If the packet does not reach its destination after being forwarded the specified number of times, the packet is discarded and the ICMP echo request fails. When this happens, the Status is set to TtlExpired.

The following code example demonstrates calling this method.

        public static void SimplePing ()
        {
            Ping pingSender = new Ping ();
            PingReply reply = pingSender.Send ("www.contoso.com");

            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);
            }
            else
            {
                Console.WriteLine (reply.Status);
            }
        }

.NET Framework

Supported in: 4.6, 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.

Show:
© 2014 Microsoft