Dns.BeginGetHostEntry 方法

定义

将主机名或 IP 地址异步解析为 IPHostEntry 实例。Asynchronously resolves a host name or IP address to an IPHostEntry instance.

重载

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

将 IP 地址异步解析为 IPHostEntry 实例。Asynchronously resolves an IP address to an IPHostEntry instance.

BeginGetHostEntry(String, AsyncCallback, Object)

将主机名或 IP 地址异步解析为 IPHostEntry 实例。Asynchronously resolves a host name or IP address to an IPHostEntry instance.

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

将 IP 地址异步解析为 IPHostEntry 实例。Asynchronously resolves an IP address to an IPHostEntry instance.

public:
 static IAsyncResult ^ BeginGetHostEntry(System::Net::IPAddress ^ address, AsyncCallback ^ requestCallback, System::Object ^ stateObject);
public static IAsyncResult BeginGetHostEntry (System.Net.IPAddress address, AsyncCallback? requestCallback, object? stateObject);
public static IAsyncResult BeginGetHostEntry (System.Net.IPAddress address, AsyncCallback requestCallback, object stateObject);
static member BeginGetHostEntry : System.Net.IPAddress * AsyncCallback * obj -> IAsyncResult
Public Shared Function BeginGetHostEntry (address As IPAddress, requestCallback As AsyncCallback, stateObject As Object) As IAsyncResult

参数

address
IPAddress

要解析的 IP 地址。The IP address to resolve.

requestCallback
AsyncCallback

一个 AsyncCallback 委托,它引用操作完成时要调用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

stateObject
Object

一个用户定义对象,其中包含操作的相关信息。A user-defined object that contains information about the operation. 当操作完成时,此对象会被传递给 requestCallback 委托。This object is passed to the requestCallback delegate when the operation is complete.

返回

IAsyncResult

引用异步请求的 IAsyncResult 实例。An IAsyncResult instance that references the asynchronous request.

例外

addressnulladdress is null.

解析 address 时遇到错误。An error is encountered when resolving address.

address 是无效的 IP 地址。address is an invalid IP address.

示例

下面的代码示例使用 BeginGetHostEntry 方法将 IP 地址解析为 IPHostEntry 实例。The following code example uses the BeginGetHostEntry method to resolve an IP address to an IPHostEntry instance.

    // Signals when the resolve has finished.
public:
    static ManualResetEvent^ GetHostEntryFinished =
        gcnew ManualResetEvent(false);

    // define the state object for the callback. 
    // use hostName to correlate calls with the proper result.
    ref class ResolveState
    {
    public:
        String^ hostName;
        IPHostEntry^ resolvedIPs;

        ResolveState(String^ host)
        {
            hostName = host;
        }

        property IPHostEntry^ IPs 
        {
            IPHostEntry^ get()
            {
                return resolvedIPs;
            }

            void set(IPHostEntry^ IPs)
            {
                resolvedIPs = IPs;
            }
        }

        property String^ host 
        {
            String^ get()
            {
                return hostName;
            }

            void set(String^ host)
            {
                hostName = host;
            }
        }
    };

    // Record the IPs in the state object for later use.
    static void GetHostEntryCallback(IAsyncResult^ ar)
    {
        ResolveState^ ioContext = (ResolveState^)(ar->AsyncState);

        ioContext->IPs = Dns::EndGetHostEntry(ar);
        GetHostEntryFinished->Set();
    }


    // Determine the Internet Protocol(IP) addresses for this 
    // host asynchronously.
public:
    static void DoGetHostEntryAsync(String^ hostName)
    {
        GetHostEntryFinished->Reset();
        ResolveState^ ioContext = gcnew ResolveState(hostName);

        Dns::BeginGetHostEntry(ioContext->host,
            gcnew AsyncCallback(GetHostEntryCallback), ioContext);
        // Wait here until the resolve completes 
        // (the callback calls .Set())
        GetHostEntryFinished->WaitOne();

        Console::WriteLine("EndGetHostEntry({0}) returns:", ioContext->host);
      
        for (int i = 0; i < ioContext->IPs->AddressList->Length; i++)
        {
            Console::WriteLine("    {0}", ioContext->IPs->AddressList[i]->ToString());
        }

//      for each (IPAddress^ address in ioContext->IPs)
//      {
//          Console::WriteLine("{0} ", address);
//      }
    }
// Signals when the resolve has finished.
public static ManualResetEvent GetHostEntryFinished =
    new ManualResetEvent(false);

// Define the state object for the callback.
// Use hostName to correlate calls with the proper result.
public class ResolveState
{
    string hostName;
    IPHostEntry resolvedIPs;

    public ResolveState(string host)
    {
        hostName = host;
    }

    public IPHostEntry IPs
    {
        get { return resolvedIPs; }
        set { resolvedIPs = value; }
    }

    public string host
    {
        get { return hostName; }
        set { hostName = value; }
    }
}

// Record the IPs in the state object for later use.
public static void GetHostEntryCallback(IAsyncResult ar)
{
    ResolveState ioContext = (ResolveState)ar.AsyncState;
    ioContext.IPs = Dns.EndGetHostEntry(ar);
    GetHostEntryFinished.Set();
}

// Determine the Internet Protocol (IP) addresses for
// this host asynchronously.
public static void DoGetHostEntryAsync(string hostname)
{
    GetHostEntryFinished.Reset();
    ResolveState ioContext= new ResolveState(hostname);

    Dns.BeginGetHostEntry(ioContext.host,
        new AsyncCallback(GetHostEntryCallback), ioContext);

    // Wait here until the resolve completes (the callback
    // calls .Set())
    GetHostEntryFinished.WaitOne();

    Console.WriteLine("EndGetHostEntry({0}) returns:", ioContext.host);

    foreach (IPAddress address in ioContext.IPs.AddressList)
    {
        Console.WriteLine($"    {address}");
    }
}
' Signals when the resolve has finished.
Dim Shared GetHostEntryFinished As ManualResetEvent = New ManualResetEvent(False)

' Define the state object for the callback. 
' Use hostName to correlate calls with the proper result.
Class ResolveState
    
    Dim hostName As String
    Dim resolvedIPs As IPHostEntry

    Public Sub New(host As String)
        hostName = host
    End Sub

    Public Property IPs AS IPHostEntry
        Get
            Return resolvedIPs
        End Get
        Set
            resolvedIPs = value
        End Set
    End Property

    Public Property host As String
        Get
            Return hostName
        End Get
        Set
            hostName = value
        End Set
    End Property

End Class

' Record the IPs in the state object for later use.
Shared Sub GetHostEntryCallback(ar As IAsyncResult)

    Dim ioContext As ResolveState = ar.AsyncState

    ioContext.IPs = Dns.EndGetHostEntry(ar)
    GetHostEntryFinished.Set()

End Sub

' Determine the Internet Protocol (IP) addresses for 
' this host asynchronously.
Shared Sub DoGetHostEntryAsync(hostname As String)
    
    GetHostEntryFinished.Reset()
    Dim ioContext As ResolveState = New ResolveState(hostname)

    Dns.BeginGetHostEntry(ioContext.host,AddressOf GetHostEntryCallback, ioContext)

    ' Wait here until the resolve completes (the callback 
    ' calls .Set())
    GetHostEntryFinished.WaitOne()

    Console.WriteLine($"EndGetHostEntry({ioContext.host}) returns:")

    Dim addresses As IPAddress() = ioContext.IPs.AddressList

    Dim index As Integer
    For index = 0 To addresses.Length - 1
        Console.WriteLine($"    {addresses(index)}")
    Next index

End Sub

注解

BeginGetHostEntry方法以异步方式在 DNS 服务器中查询与 ip 地址关联的 ip 地址和别名。The BeginGetHostEntry method asynchronously queries a DNS server for the IP addresses and aliases associated with an IP address.

注意 在应用程序中启用网络跟踪后,此成员将发出跟踪信息。Note This member emits trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

BeginGetHostEntry必须通过调用方法完成异步操作 EndGetHostEntryThe asynchronous BeginGetHostEntry operation must be completed by calling the EndGetHostEntry method. 通常,方法由 requestCallback 委托调用。Typically, the method is invoked by the requestCallback delegate.

在操作完成之前,此方法不会被阻止。This method does not block until the operation is complete. 若要在操作完成之前一直阻止,请使用 GetHostEntry 方法。To block until the operation is complete, use the GetHostEntry method.

有关使用异步编程模型的详细信息,请参阅 以异步方式调用同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

适用于

BeginGetHostEntry(String, AsyncCallback, Object)

将主机名或 IP 地址异步解析为 IPHostEntry 实例。Asynchronously resolves a host name or IP address to an IPHostEntry instance.

public:
 static IAsyncResult ^ BeginGetHostEntry(System::String ^ hostNameOrAddress, AsyncCallback ^ requestCallback, System::Object ^ stateObject);
public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress, AsyncCallback? requestCallback, object? stateObject);
public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress, AsyncCallback requestCallback, object stateObject);
static member BeginGetHostEntry : string * AsyncCallback * obj -> IAsyncResult
Public Shared Function BeginGetHostEntry (hostNameOrAddress As String, requestCallback As AsyncCallback, stateObject As Object) As IAsyncResult

参数

hostNameOrAddress
String

要解析的主机名或 IP 地址。The host name or IP address to resolve.

requestCallback
AsyncCallback

一个 AsyncCallback 委托,它引用操作完成时要调用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

stateObject
Object

一个用户定义对象,其中包含操作的相关信息。A user-defined object that contains information about the operation. 当操作完成时,此对象会被传递给 requestCallback 委托。This object is passed to the requestCallback delegate when the operation is complete.

返回

IAsyncResult

引用异步请求的 IAsyncResult 实例。An IAsyncResult instance that references the asynchronous request.

例外

hostNameOrAddressnullhostNameOrAddress is null.

hostNameOrAddress 的长度超过 255 个字符。The length of hostNameOrAddress is greater than 255 characters.

解析 hostNameOrAddress 时遇到错误。An error is encountered when resolving hostNameOrAddress.

hostNameOrAddress 是无效的 IP 地址。hostNameOrAddress is an invalid IP address.

示例

下面的代码示例使用 BeginGetHostEntry 方法将 IP 地址解析为 IPHostEntry 实例。The following code example uses the BeginGetHostEntry method to resolve an IP address to an IPHostEntry instance.

    // Signals when the resolve has finished.
public:
    static ManualResetEvent^ GetHostEntryFinished =
        gcnew ManualResetEvent(false);

    // define the state object for the callback. 
    // use hostName to correlate calls with the proper result.
    ref class ResolveState
    {
    public:
        String^ hostName;
        IPHostEntry^ resolvedIPs;

        ResolveState(String^ host)
        {
            hostName = host;
        }

        property IPHostEntry^ IPs 
        {
            IPHostEntry^ get()
            {
                return resolvedIPs;
            }

            void set(IPHostEntry^ IPs)
            {
                resolvedIPs = IPs;
            }
        }

        property String^ host 
        {
            String^ get()
            {
                return hostName;
            }

            void set(String^ host)
            {
                hostName = host;
            }
        }
    };

    // Record the IPs in the state object for later use.
    static void GetHostEntryCallback(IAsyncResult^ ar)
    {
        ResolveState^ ioContext = (ResolveState^)(ar->AsyncState);

        ioContext->IPs = Dns::EndGetHostEntry(ar);
        GetHostEntryFinished->Set();
    }


    // Determine the Internet Protocol(IP) addresses for this 
    // host asynchronously.
public:
    static void DoGetHostEntryAsync(String^ hostName)
    {
        GetHostEntryFinished->Reset();
        ResolveState^ ioContext = gcnew ResolveState(hostName);

        Dns::BeginGetHostEntry(ioContext->host,
            gcnew AsyncCallback(GetHostEntryCallback), ioContext);
        // Wait here until the resolve completes 
        // (the callback calls .Set())
        GetHostEntryFinished->WaitOne();

        Console::WriteLine("EndGetHostEntry({0}) returns:", ioContext->host);
      
        for (int i = 0; i < ioContext->IPs->AddressList->Length; i++)
        {
            Console::WriteLine("    {0}", ioContext->IPs->AddressList[i]->ToString());
        }

//      for each (IPAddress^ address in ioContext->IPs)
//      {
//          Console::WriteLine("{0} ", address);
//      }
    }
// Signals when the resolve has finished.
public static ManualResetEvent GetHostEntryFinished =
    new ManualResetEvent(false);

// Define the state object for the callback.
// Use hostName to correlate calls with the proper result.
public class ResolveState
{
    string hostName;
    IPHostEntry resolvedIPs;

    public ResolveState(string host)
    {
        hostName = host;
    }

    public IPHostEntry IPs
    {
        get { return resolvedIPs; }
        set { resolvedIPs = value; }
    }

    public string host
    {
        get { return hostName; }
        set { hostName = value; }
    }
}

// Record the IPs in the state object for later use.
public static void GetHostEntryCallback(IAsyncResult ar)
{
    ResolveState ioContext = (ResolveState)ar.AsyncState;
    ioContext.IPs = Dns.EndGetHostEntry(ar);
    GetHostEntryFinished.Set();
}

// Determine the Internet Protocol (IP) addresses for
// this host asynchronously.
public static void DoGetHostEntryAsync(string hostname)
{
    GetHostEntryFinished.Reset();
    ResolveState ioContext= new ResolveState(hostname);

    Dns.BeginGetHostEntry(ioContext.host,
        new AsyncCallback(GetHostEntryCallback), ioContext);

    // Wait here until the resolve completes (the callback
    // calls .Set())
    GetHostEntryFinished.WaitOne();

    Console.WriteLine("EndGetHostEntry({0}) returns:", ioContext.host);

    foreach (IPAddress address in ioContext.IPs.AddressList)
    {
        Console.WriteLine($"    {address}");
    }
}
' Signals when the resolve has finished.
Dim Shared GetHostEntryFinished As ManualResetEvent = New ManualResetEvent(False)

' Define the state object for the callback. 
' Use hostName to correlate calls with the proper result.
Class ResolveState
    
    Dim hostName As String
    Dim resolvedIPs As IPHostEntry

    Public Sub New(host As String)
        hostName = host
    End Sub

    Public Property IPs AS IPHostEntry
        Get
            Return resolvedIPs
        End Get
        Set
            resolvedIPs = value
        End Set
    End Property

    Public Property host As String
        Get
            Return hostName
        End Get
        Set
            hostName = value
        End Set
    End Property

End Class

' Record the IPs in the state object for later use.
Shared Sub GetHostEntryCallback(ar As IAsyncResult)

    Dim ioContext As ResolveState = ar.AsyncState

    ioContext.IPs = Dns.EndGetHostEntry(ar)
    GetHostEntryFinished.Set()

End Sub

' Determine the Internet Protocol (IP) addresses for 
' this host asynchronously.
Shared Sub DoGetHostEntryAsync(hostname As String)
    
    GetHostEntryFinished.Reset()
    Dim ioContext As ResolveState = New ResolveState(hostname)

    Dns.BeginGetHostEntry(ioContext.host,AddressOf GetHostEntryCallback, ioContext)

    ' Wait here until the resolve completes (the callback 
    ' calls .Set())
    GetHostEntryFinished.WaitOne()

    Console.WriteLine($"EndGetHostEntry({ioContext.host}) returns:")

    Dim addresses As IPAddress() = ioContext.IPs.AddressList

    Dim index As Integer
    For index = 0 To addresses.Length - 1
        Console.WriteLine($"    {addresses(index)}")
    Next index

End Sub

注解

BeginGetHostEntry方法在 DNS 服务器中查询与主机名或 ip 地址关联的 ip 地址。The BeginGetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.

注意 在应用程序中启用网络跟踪后,此成员将发出跟踪信息。Note This member emits trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

BeginGetHostEntry必须通过调用方法完成异步操作 EndGetHostEntryThe asynchronous BeginGetHostEntry operation must be completed by calling the EndGetHostEntry method. 通常,方法由 requestCallback 委托调用。Typically, the method is invoked by the requestCallback delegate.

在操作完成之前,此方法不会被阻止。This method does not block until the operation is complete. 若要在操作完成之前一直阻止,请使用 GetHostEntry 方法。To block until the operation is complete, use the GetHostEntry method.

有关使用异步编程模型的详细信息,请参阅 以异步方式调用同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

适用于