Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Ping.Send Method (String, Int32, Byte[], PingOptions)

Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer, and receive a corresponding ICMP echo reply message from that computer. This overload allows you to specify a time-out value for the operation and control fragmentation and Time-to-Live values for the ICMP packet.

Namespace:  System.Net.NetworkInformation
Assembly:  System (in System.dll)
public PingReply Send(
	string hostNameOrAddress,
	int timeout,
	byte[] buffer,
	PingOptions options
)

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.
timeout
Type: System.Int32
An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.
buffer
Type: System.Byte[]
A Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
options
Type: System.Net.NetworkInformation.PingOptions
A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.

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
ArgumentException

The size of buffer exceeds 65500 bytes.

ArgumentNullException

hostNameOrAddress is null or is a zero length string.

-or-

buffer is null, or the buffer size is greater than 65500 bytes.

ArgumentOutOfRangeException

timeout is less than zero.

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.

If the ICMP echo reply message is not received within the time specified by the timeout parameter, the ICMP echo fails, and the Status property is set to TimedOut.

NoteNote

When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

If the DontFragment property 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.

Use the Ttl property to specify the maximum number of times the ICMP echo message can be forwarded before reaching its destination. 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 how to call this method.


        public static void ComplexPing ()
        {
            Ping pingSender = new Ping ();

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);

            // Wait 10 seconds for a reply.
            int timeout = 10000;

            // Set options for transmission:
            // The data can go through 64 gateways or routers
            // before it is destroyed, and the data packet
            // cannot be fragmented.
            PingOptions options = new PingOptions (64, true);

            // Send the request.
            PingReply reply = pingSender.Send ("www.contoso.com", 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);
            }
            else
            {
                Console.WriteLine (reply.Status);
            }
        }



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.