Export (0) Print
Expand All

IPAddress.IsLoopback Method

Indicates whether the specified IP address is the loopback address.

[Visual Basic]
Public Shared Function IsLoopback( _
   ByVal address As IPAddress _
) As Boolean
[C#]
public static bool IsLoopback(
 IPAddress address
);
[C++]
public: static bool IsLoopback(
 IPAddress* address
);
[JScript]
public static function IsLoopback(
   address : IPAddress
) : Boolean;

Parameters

address
An IP address.

Return Value

true if address is the loopback address; otherwise, false.

Remarks

The IsLoopback method compares address to Loopback and returns true if the two IP addresses are the same.

Notice, in the case of IPv4, the IsLoopback returns true for any IP address of the form 127.X.Y.Z (where X, Y, and Z are in the range 0-255), not just Loopback (127.0.0.1).

Example

[Visual Basic, C#, C++] The following code example uses the IsLoopback property to determine whether the specified address is a loopback address.

[Visual Basic] 
Imports System
Imports System.Net
Imports System.Net.Sockets

 _

Class IsLoopbackTest
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   
   Overloads Private Shared Sub Main(args() As String)
      
       If args.Length = 1 Then
         ' No parameters entered. Display program usage.
         Console.WriteLine("Please enter an IP address.")
         Console.WriteLine("Usage:   >ipaddress_isloopback any IPv4 or IPv6 address.")
         Console.WriteLine("Example: >ipaddress_isloopback 127.0.0.1")
         Console.WriteLine("Example: >ipaddress_isloopback 0:0:0:0:0:0:0:1")
         Return
      ' Parse the address string entered by the user.
      Else
        parse(args(1))
      End If 
   End Sub 'Main
   
   
   ' This method calls the IPAddress.Parse method to check whether the 
   ' passed ipAddress parameter is in the correct format.
   ' Then it checks whether it represents a loopback address.
   ' Finally, it displays the results.
   Private Shared Sub parse(ipAdd As String)
      Dim loopBack As String = " is not a loopback address."
      
      Try
         ' Perform syntax check by parsing the address string entered by the user.
         Dim address As IPAddress = IPAddress.Parse(ipAdd)
         
         ' Perform semantic check by verifying that the address is a valid IPv4 
         ' or IPv6 loopback address. 
         If IPAddress.IsLoopback(address) And address.AddressFamily = AddressFamily.InterNetworkV6 Then
            loopBack = " is an IPv6 loopback address " + "whose internal format is: " + address.ToString() + "."
         Else
            If IPAddress.IsLoopback(address) And address.AddressFamily = AddressFamily.InterNetwork Then
               loopBack = " is an IPv4 loopback address " + "whose internal format is: " + address.ToString() + "."
            End If
         End If 
         ' Display the results.
         Console.WriteLine(("Your input address: " + """" + ipAdd + """" + loopBack))
      
      Catch e As FormatException
         Console.WriteLine("FormatException caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      
      Catch e As Exception
         Console.WriteLine("Exception caught!!!")
         Console.WriteLine(("Source : " + e.Source))
         Console.WriteLine(("Message : " + e.Message))
      End Try
   End Sub 'parse 
End Class 'IsLoopbackTest

[C#] 

using System;
using System.Net;
using System.Net.Sockets;

class IsLoopbackTest
{

  private static void Main(string[] args) 
  {
    
    if (args.Length == 0)
    {
      // No parameters entered. Display program usage.
      Console.WriteLine("Please enter an IP address.");
      Console.WriteLine("Usage:   >ipaddress_isloopback any IPv4 or IPv6 address.");
      Console.WriteLine("Example: >ipaddress_isloopback 127.0.0.1");
      Console.WriteLine("Example: >ipaddress_isloopback 0:0:0:0:0:0:0:1");
      return;
    }
    else
      // Parse the address string entered by the user.
      parse(args[0]);
   
  }

  // This method calls the IPAddress.Parse method to check if the 
  // passed ipAddress parameter is in the correct format.
  // Then it checks whether it represents a loopback address.
  // Finally, it displays the results.
  private static void parse(string ipAddress)
  {
    string loopBack=" is not a loopback address.";

    try
    {
      // Perform syntax check by parsing the address string entered by the user.
      IPAddress address = IPAddress.Parse(ipAddress);

      // Perform semantic check by verifying that the address is a valid IPv4 
      // or IPv6 loopback address. 
      if(IPAddress.IsLoopback(address)&& address.AddressFamily == AddressFamily.InterNetworkV6)
        loopBack =  " is an IPv6 loopback address " +
                    "whose internal format is: " + address.ToString() + ".";
      else
        if(IPAddress.IsLoopback(address) && address.AddressFamily == AddressFamily.InterNetwork)
          loopBack = " is an IPv4 loopback address " +
                     "whose internal format is: " + address.ToString() + ".";

      // Display the results.
      Console.WriteLine("Your input address: " + "\"" + ipAddress + "\"" + loopBack);
    }

    catch(FormatException e)
    {
      Console.WriteLine("FormatException caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
    
    catch(Exception e)
    {
      Console.WriteLine("Exception caught!!!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }

   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;

// This method calls the IPAddress::Parse method to check if the
// passed ipAddress parameter is in the correct format.
// Then it checks whether it represents a loopback address.
// Finally, it displays the results.
void parse(String* ipAddress) {
   String* loopBack=S" is not a loopback address.";
   try {
      // Perform syntax check by parsing the address string entered by the user.
      IPAddress* address = IPAddress::Parse(ipAddress);

      // Perform semantic check by verifying that the address is a valid IPv4
      // or IPv6 loopback address.
      if (IPAddress::IsLoopback(address)&&
         address->AddressFamily == AddressFamily::InterNetworkV6)
         loopBack = String::Concat(S" is an IPv6 loopback address whose internal format is: ", address, S".");
      else
         if (IPAddress::IsLoopback(address) &&
            address->AddressFamily == AddressFamily::InterNetwork)
            loopBack = String::Concat(S" is an IPv4 loopback address whose internal format is: ", address, S".");

      // Display the results.
      Console::WriteLine(S"Your input address: \" {0} \" {1}", ipAddress, loopBack);
   } catch (FormatException* e) {
      Console::WriteLine(S"FormatException caught!!!");
      Console::WriteLine(S"Source : {0}", e->Source);
      Console::WriteLine(S"Message : {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception caught!!!");
      Console::WriteLine(S"Source : {0}", e->Source);
      Console::WriteLine(S"Message : {0}", e->Message);
   }
}

int main() {
   String* args[] = Environment::GetCommandLineArgs();
   if (args->Length == 1) {
      // No parameters entered. Display program usage.
      Console::WriteLine(S"Please enter an IP address.");
      Console::WriteLine(S"Usage:   >ipaddress_isloopback any IPv4 or IPv6 address.");
      Console::WriteLine(S"Example: >ipaddress_isloopback 127.0.0.1");
      Console::WriteLine(S"Example: >ipaddress_isloopback 0:0:0:0:0:0:0:1");
   } else
      // Parse the address string entered by the user.
      parse(args[1]);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

IPAddress Class | IPAddress Members | System.Net Namespace

Show:
© 2014 Microsoft