Ping.Send Ping.Send Ping.Send Ping.Send Method

Definition

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

Overloads

Send(String, Int32, Byte[], PingOptions) Send(String, Int32, Byte[], PingOptions) Send(String, Int32, Byte[], PingOptions) Send(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.

Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions)

Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress 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 echo message packet.

Send(String, Int32, Byte[]) Send(String, Int32, Byte[]) Send(String, Int32, Byte[]) Send(String, Int32, Byte[])

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.

Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[])

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

Send(String, Int32) Send(String, Int32) Send(String, Int32) Send(String, Int32)

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. This method allows you to specify a time-out value for the operation.

Send(IPAddress, Int32) Send(IPAddress, Int32) Send(IPAddress, Int32) Send(IPAddress, Int32)

Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. This method allows you to specify a time-out value for the operation.

Send(String) Send(String) Send(String) Send(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.

Send(IPAddress) Send(IPAddress) Send(IPAddress) Send(IPAddress)

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

Remarks

The IP address returned by any of the Send overloads can originate from a malicious remote computer. Do not connect to the remote computer using this. Use DNS to determine the IP address of the machine to which you want to connect.

Send(String, Int32, Byte[], PingOptions) Send(String, Int32, Byte[], PingOptions) Send(String, Int32, Byte[], PingOptions) Send(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.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::String ^ hostNameOrAddress, int timeout, cli::array <System::Byte> ^ buffer, System::Net::NetworkInformation::PingOptions ^ options);
public System.Net.NetworkInformation.PingReply Send (string hostNameOrAddress, int timeout, byte[] buffer, System.Net.NetworkInformation.PingOptions options);
member this.Send : string * int * byte[] * System.Net.NetworkInformation.PingOptions -> System.Net.NetworkInformation.PingReply
Public Function Send (hostNameOrAddress As String, timeout As Integer, buffer As Byte(), options As PingOptions) As PingReply
Parameters
hostNameOrAddress
String String String 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
Int32 Int32 Int32 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
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
PingOptions PingOptions PingOptions PingOptions

A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.

Returns

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.

Exceptions

hostNameOrAddress is null or is a zero length string.

-or-

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

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

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

Examples

The following code example demonstrates how to call this method.

void ComplexPing()
{
   Ping ^ pingSender = gcnew Ping;
   
   // Create a buffer of 32 bytes of data to be transmitted.
   String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   array<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 = gcnew 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 );
   }
}


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

Remarks

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.

Note

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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions) Send(IPAddress, Int32, Byte[], PingOptions)

Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress 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 echo message packet.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::Net::IPAddress ^ address, int timeout, cli::array <System::Byte> ^ buffer, System::Net::NetworkInformation::PingOptions ^ options);
public System.Net.NetworkInformation.PingReply Send (System.Net.IPAddress address, int timeout, byte[] buffer, System.Net.NetworkInformation.PingOptions options);
member this.Send : System.Net.IPAddress * int * byte[] * System.Net.NetworkInformation.PingOptions -> System.Net.NetworkInformation.PingReply
Public Function Send (address As IPAddress, timeout As Integer, buffer As Byte(), options As PingOptions) As PingReply
Parameters
address
IPAddress IPAddress IPAddress IPAddress

An IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32 Int32 Int32 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
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
PingOptions PingOptions PingOptions PingOptions

A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.

Returns

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. The method will return PacketTooBig if the packet exceeds the Maximum Transmission Unit (MTU).

Exceptions

address is null.

-or-

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

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

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

Examples

The following code example demonstrates how to call this method.

void ComplexLocalPing()
{
   
   // Ping's the local machine.
   Ping ^ pingSender = gcnew Ping;
   IPAddress^ address = IPAddress::Loopback;
   
   // Create a buffer of 32 bytes of data to be transmitted.
   String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   array<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 = gcnew PingOptions( 64,true );
   PingReply ^ reply = pingSender->Send( address, 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 );
   }
}


public static void ComplexLocalPing ()
{
    // Ping's the local machine.
    Ping pingSender = new Ping ();
    IPAddress address = IPAddress.Loopback;

    // 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);
    PingReply reply = pingSender.Send (address, 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);
    }
}

Remarks

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.

Note

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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(String, Int32, Byte[]) Send(String, Int32, Byte[]) Send(String, Int32, Byte[]) Send(String, Int32, Byte[])

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.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::String ^ hostNameOrAddress, int timeout, cli::array <System::Byte> ^ buffer);
[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public System.Net.NetworkInformation.PingReply Send (string hostNameOrAddress, int timeout, byte[] buffer);
member this.Send : string * int * byte[] -> System.Net.NetworkInformation.PingReply
Public Function Send (hostNameOrAddress As String, timeout As Integer, buffer As Byte()) As PingReply
Parameters
hostNameOrAddress
String String String 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
Int32 Int32 Int32 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
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.

Returns

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.

Exceptions

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

-or-

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

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

Examples

For an example that demonstrates how to call a Send overload, see the Ping class overview.

Remarks

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

Note

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

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 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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[]) Send(IPAddress, Int32, Byte[])

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

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::Net::IPAddress ^ address, int timeout, cli::array <System::Byte> ^ buffer);
[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public System.Net.NetworkInformation.PingReply Send (System.Net.IPAddress address, int timeout, byte[] buffer);
member this.Send : System.Net.IPAddress * int * byte[] -> System.Net.NetworkInformation.PingReply
Public Function Send (address As IPAddress, timeout As Integer, buffer As Byte()) As PingReply
Parameters
address
IPAddress IPAddress IPAddress IPAddress

An IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32 Int32 Int32 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
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.

Returns

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. The method will return PacketTooBig if the packet exceeds the Maximum Transmission Unit (MTU).

Exceptions

address is null.

-or-

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

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

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

Examples

The following code example demonstrates how to call this method.

void LocalPingTimeout()
{
   
   // Ping's the local machine.
   Ping ^ pingSender = gcnew Ping;
   IPAddress^ address = IPAddress::Loopback;
   
   // Create a buffer of 32 bytes of data to be transmitted.
   String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
   array<Byte>^buffer = Encoding::ASCII->GetBytes( data );
   
   // Wait 10 seconds for a reply.
   int timeout = 10000;
   PingReply ^ reply = pingSender->Send( address, timeout, buffer);
   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 );
   }
}


public static void LocalPingTimeout ()
{
    // Ping's the local machine.
    Ping pingSender = new Ping ();
    IPAddress address = IPAddress.Loopback;

    // 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;
    PingReply reply = pingSender.Send (address, timeout, buffer);

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

Remarks

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

Note

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

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 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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(String, Int32) Send(String, Int32) Send(String, Int32) Send(String, Int32)

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. This method allows you to specify a time-out value for the operation.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::String ^ hostNameOrAddress, int timeout);
public System.Net.NetworkInformation.PingReply Send (string hostNameOrAddress, int timeout);
member this.Send : string * int -> System.Net.NetworkInformation.PingReply
Public Function Send (hostNameOrAddress As String, timeout As Integer) As PingReply
Parameters
hostNameOrAddress
String String String 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
Int32 Int32 Int32 Int32

An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

Returns

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.

Exceptions

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

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

Examples

For an example that demonstrates calling a Send method, see the Ping class overview.

Remarks

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

Note

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

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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(IPAddress, Int32) Send(IPAddress, Int32) Send(IPAddress, Int32) Send(IPAddress, Int32)

Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. This method allows you to specify a time-out value for the operation.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::Net::IPAddress ^ address, int timeout);
public System.Net.NetworkInformation.PingReply Send (System.Net.IPAddress address, int timeout);
member this.Send : System.Net.IPAddress * int -> System.Net.NetworkInformation.PingReply
Public Function Send (address As IPAddress, timeout As Integer) As PingReply
Parameters
address
IPAddress IPAddress IPAddress IPAddress

An IPAddress that identifies the computer that is the destination for the ICMP echo message.

timeout
Int32 Int32 Int32 Int32

An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.

Returns

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.

Exceptions

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

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

Examples

For an example that demonstrates calling a Send method, see the Ping class overview.

Remarks

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

Note

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

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 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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(String) Send(String) Send(String) Send(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.

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::String ^ hostNameOrAddress);
public System.Net.NetworkInformation.PingReply Send (string hostNameOrAddress);
member this.Send : string -> System.Net.NetworkInformation.PingReply
Public Function Send (hostNameOrAddress As String) As PingReply
Parameters
hostNameOrAddress
String String String 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.

Returns

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.

Exceptions

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

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

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

Examples

The following code example demonstrates calling this method.

void SimplePing()
{
   Ping ^ pingSender = gcnew 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 );
   }
}


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

Remarks

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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Send(IPAddress) Send(IPAddress) Send(IPAddress) Send(IPAddress)

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

public:
 System::Net::NetworkInformation::PingReply ^ Send(System::Net::IPAddress ^ address);
public System.Net.NetworkInformation.PingReply Send (System.Net.IPAddress address);
member this.Send : System.Net.IPAddress -> System.Net.NetworkInformation.PingReply
Public Function Send (address As IPAddress) As PingReply
Parameters
address
IPAddress IPAddress IPAddress IPAddress

An IPAddress that identifies the computer that is the destination for the ICMP echo message.

Returns

A PingReply object that provides information about the ICMP echo reply message, if one was received, or describes the reason for the failure if no message was received.

Exceptions

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

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

Examples

The following code example demonstrates calling this method.

void LocalPing()
{
   
   // Ping's the local machine.
   Ping ^ pingSender = gcnew Ping;
   IPAddress^ address = IPAddress::Loopback;
   PingReply ^ reply = pingSender->Send( address );
   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 );
   }
}


public static void LocalPing ()
{
    // Ping's the local machine.
    Ping pingSender = new Ping ();
    IPAddress address = IPAddress.Loopback;
    PingReply reply = pingSender.Send (address);

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

Remarks

This method sends to the host that is specified by address 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.

Security

NetworkInformationPermission
To send an ICMP echo message. Associated enumeration: Ping

Applies to