Export (0) Print
Expand All

IPAddress.IsLoopback Method

Indicates whether the specified IP address is the loopback address.

Namespace: System.Net
Assembly: System (in system.dll)

public static bool IsLoopback (
	IPAddress address
)
public static boolean IsLoopback (
	IPAddress address
)
public static function IsLoopback (
	address : IPAddress
) : boolean

Parameters

address

An IP address.

Return Value

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

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

Notice, in the case of IPv4, that the IsLoopback method 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).

The following code example uses the IsLoopback method to determine whether the specified address is a loopback address.


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

   }
}

import System.*;
import System.Net.*;
import System.Net.Sockets.*;

class IsLoopbackTest
{
	public 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]);
		} 
	} //main
    
	// 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.get_AddressFamily().
				Equals(AddressFamily.InterNetworkV6)) {
				loopBack = " is an IPv6 loopback address "
					+ "whose internal format is: " + address.ToString() + ".";
			}
			else {
				if (IPAddress.IsLoopback(address) && 
					address.get_AddressFamily().Equals
					(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.get_Source()));
			Console.WriteLine(("Message : " + e.get_Message()));
		}
   		catch (System.Exception e) {
			Console.WriteLine("Exception caught!!!");
			Console.WriteLine(("Source : " + e.get_Source()));
			Console.WriteLine(("Message : " + e.get_Message()));
		}
	} //Parse
} //IsLoopbackTest

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft