다음을 통해 공유


IPAddress.IsLoopback(IPAddress) 메서드

정의

지정된 IP 주소가 루프백 주소인지 여부를 나타냅니다.

public:
 static bool IsLoopback(System::Net::IPAddress ^ address);
public static bool IsLoopback (System.Net.IPAddress address);
static member IsLoopback : System.Net.IPAddress -> bool
Public Shared Function IsLoopback (address As IPAddress) As Boolean

매개 변수

address
IPAddress

IP 주소.

반환

address가 루프백 주소이면 true이고, 그렇지 않으면 false입니다.

예제

다음 코드 예제에서는 메서드를 IsLoopback 사용하여 지정된 주소가 루프백 주소인지 여부를 확인합니다.

#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 = " 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( " is an IPv6 loopback address whose internal format is: ", address, "." );
      else
      if ( IPAddress::IsLoopback( address ) && address->AddressFamily == AddressFamily::InterNetwork )
            loopBack = String::Concat( " is an IPv4 loopback address whose internal format is: ", address, "." );
      
      // Display the results.
      Console::WriteLine( "Your input address: \" {0} \" {1}", ipAddress, loopBack );
   }
   catch ( FormatException^ e ) 
   {
      Console::WriteLine( "FormatException caught!!!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception caught!!!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }

}

int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   if ( args->Length == 1 )
   {
      
      // 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" );
   }
   else
      parse( args[ 1 ] );

   
   // Parse the address string entered by the user.
}
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);
    }
   }
}
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
   
   
   ' 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
End Class

설명

메서드는 IsLoopback 두 IP 주소가 addressLoopback 동일한 경우 를 비교하고 를 반환 true 합니다.

IPv4의 경우 메서드는 IsLoopback (127.0.0.1) 뿐만 Loopback 아니라 127.X.Y.Z(X, Y 및 Z가 0-255 범위에 있는 경우) 양식의 IP 주소에 대해 를 반환 true 합니다.

적용 대상