Dns.BeginGetHostEntry Metod

Definition

Asynkront löser ett värdnamn eller en IP-adress till en IPHostEntry instans.

Överlagringar

Name Description
BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Asynkront löser en IP-adress till en IPHostEntry instans.

BeginGetHostEntry(String, AsyncCallback, Object)

Asynkront löser ett värdnamn eller en IP-adress till en IPHostEntry instans.

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Asynkront löser en IP-adress till en IPHostEntry instans.

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);
static member BeginGetHostEntry : System.Net.IPAddress * AsyncCallback * obj -> IAsyncResult
Public Shared Function BeginGetHostEntry (address As IPAddress, requestCallback As AsyncCallback, stateObject As Object) As IAsyncResult

Parametrar

address
IPAddress

IP-adressen som ska matchas.

requestCallback
AsyncCallback

Ett AsyncCallback ombud som refererar till den metod som ska anropas när åtgärden är klar.

stateObject
Object

Ett användardefinierat objekt som innehåller information om åtgärden. Det här objektet skickas till ombudet requestCallback när åtgärden är klar.

Returer

En IAsyncResult instans som refererar till den asynkrona begäran.

Undantag

address är null.

Ett fel påträffas när du löser address.

address är en ogiltig IP-adress.

Exempel

I följande kodexempel används BeginGetHostEntry metoden för att matcha en IP-adress till en IPHostEntry instans.

// 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

Kommentarer

Metoden BeginGetHostEntry frågar asynkront en DNS-server efter IP-adresser och alias som är associerade med en IP-adress.

Observera Den här medlemmen genererar spårningsinformation när du aktiverar nätverksspårning i ditt program. Mer information finns i Network Tracing i .NET Framework.

Den asynkrona BeginGetHostEntry åtgärden måste slutföras genom att anropa EndGetHostEntry metoden. Metoden anropas vanligtvis av ombudet requestCallback .

Den här metoden blockeras inte förrän åtgärden har slutförts. Om du vill blockera tills åtgärden har slutförts använder du GetHostEntry metoden .

Detaljerad information om hur du använder den asynkrona programmeringsmodellen finns i Anropa synkrona metoder asynkront

Gäller för

BeginGetHostEntry(String, AsyncCallback, Object)

Asynkront löser ett värdnamn eller en IP-adress till en IPHostEntry instans.

public:
 static IAsyncResult ^ BeginGetHostEntry(System::String ^ hostNameOrAddress, AsyncCallback ^ requestCallback, System::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

Parametrar

hostNameOrAddress
String

Värdnamnet eller IP-adressen som ska matchas.

requestCallback
AsyncCallback

Ett AsyncCallback ombud som refererar till den metod som ska anropas när åtgärden är klar.

stateObject
Object

Ett användardefinierat objekt som innehåller information om åtgärden. Det här objektet skickas till ombudet requestCallback när åtgärden är klar.

Returer

En IAsyncResult instans som refererar till den asynkrona begäran.

Undantag

hostNameOrAddress är null.

Längden hostNameOrAddress på är större än 255 tecken.

Ett fel påträffas när du löser hostNameOrAddress.

hostNameOrAddress är en ogiltig IP-adress.

Exempel

I följande kodexempel används BeginGetHostEntry metoden för att matcha en IP-adress till en IPHostEntry instans.

// 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

Kommentarer

Metoden BeginGetHostEntry frågar en DNS-server efter den IP-adress som är associerad med ett värdnamn eller en IP-adress.

Observera Den här medlemmen genererar spårningsinformation när du aktiverar nätverksspårning i ditt program. Mer information finns i Network Tracing i .NET Framework.

Den asynkrona BeginGetHostEntry åtgärden måste slutföras genom att anropa EndGetHostEntry metoden. Metoden anropas vanligtvis av ombudet requestCallback .

Den här metoden blockeras inte förrän åtgärden har slutförts. Om du vill blockera tills åtgärden har slutförts använder du GetHostEntry metoden .

Detaljerad information om hur du använder den asynkrona programmeringsmodellen finns i Anropa synkrona metoder asynkront.

Gäller för