語言

Dns.BeginGetHostEntry 方法

定義

非同步地將主機名稱或 IP 位址解析為實 IPHostEntry 例。

多載

名稱 Description
BeginGetHostEntry(IPAddress, AsyncCallback, Object)

非同步地將 IP 位址解析為實 IPHostEntry 例。

BeginGetHostEntry(String, AsyncCallback, Object)

非同步地將主機名稱或 IP 位址解析為實 IPHostEntry 例。

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs

非同步地將 IP 位址解析為實 IPHostEntry 例。

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 位址。

requestCallback
AsyncCallback

一個 AsyncCallback 代理,當操作完成時會參考呼叫的方法。

stateObject
Object

一個包含操作資訊的使用者定義物件。 當操作完成時,這個物件會傳給 requestCallback 代理。

傳回

一個 IAsyncResult 引用非同步請求的實例。

例外狀況

addressnull

當解析 address時會遇到錯誤。

address 是一個無效的 IP 位址。

範例

以下程式碼範例使用此 BeginGetHostEntry 方法將 IP 位址解析為實 IPHostEntry 例。

// 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 位址與別名。

當您在應用程式中啟用網路追蹤時,該成員會發出追蹤資訊。 欲了解更多資訊,請參閱.NET框架中的網絡追蹤

非同步 BeginGetHostEntry 操作必須透過呼叫該 EndGetHostEntry 方法來完成。 通常,該方法由 requestCallback 委託人執行。

此方法在操作完成前不會阻塞。 要阻擋直到操作完成,請使用該 GetHostEntry 方法。

關於使用非同步程式設計模型的詳細資訊,請參見 「非同步呼叫同步方法」

適用於

BeginGetHostEntry(String, AsyncCallback, Object)

來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs
來源:
Dns.cs

非同步地將主機名稱或 IP 位址解析為實 IPHostEntry 例。

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 位址。

requestCallback
AsyncCallback

一個 AsyncCallback 代理,當操作完成時會參考呼叫的方法。

stateObject
Object

一個包含操作資訊的使用者定義物件。 當操作完成時,這個物件會傳給 requestCallback 代理。

傳回

一個 IAsyncResult 引用非同步請求的實例。

例外狀況

hostNameOrAddressnull

長度 hostNameOrAddress 超過 255 個字元。

當解析 hostNameOrAddress時會遇到錯誤。

hostNameOrAddress 是一個無效的 IP 位址。

範例

以下程式碼範例使用此 BeginGetHostEntry 方法將 IP 位址解析為實 IPHostEntry 例。

// 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 位址。

當您在應用程式中啟用網路追蹤時,該成員會發出追蹤資訊。 欲了解更多資訊,請參閱.NET框架中的網絡追蹤

非同步 BeginGetHostEntry 操作必須透過呼叫該 EndGetHostEntry 方法來完成。 通常,該方法由 requestCallback 委託人執行。

此方法在操作完成前不會阻塞。 要阻擋直到操作完成,請使用該 GetHostEntry 方法。

關於使用非同步程式設計模型的詳細資訊,請參見 「非同步呼叫同步方法」。

適用於