Dns.BeginGetHostEntry Méthode

Définition

Résout de façon asynchrone un nom d’hôte ou une adresse IP en une IPHostEntry instance.

Surcharges

Nom Description
BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Résout de façon asynchrone une adresse IP en une IPHostEntry instance.

BeginGetHostEntry(String, AsyncCallback, Object)

Résout de façon asynchrone un nom d’hôte ou une adresse IP en une IPHostEntry instance.

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Résout de façon asynchrone une adresse IP en une 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);
static member BeginGetHostEntry : System.Net.IPAddress * AsyncCallback * obj -> IAsyncResult
Public Shared Function BeginGetHostEntry (address As IPAddress, requestCallback As AsyncCallback, stateObject As Object) As IAsyncResult

Paramètres

address
IPAddress

Adresse IP à résoudre.

requestCallback
AsyncCallback

Délégué AsyncCallback qui fait référence à la méthode à appeler une fois l’opération terminée.

stateObject
Object

Objet défini par l’utilisateur qui contient des informations sur l’opération. Cet objet est transmis au requestCallback délégué une fois l’opération terminée.

Retours

Instance IAsyncResult qui fait référence à la requête asynchrone.

Exceptions

address a la valeur null.

Une erreur est rencontrée lors de la résolution address.

address est une adresse IP non valide.

Exemples

L’exemple de code suivant utilise la BeginGetHostEntry méthode pour résoudre une adresse IP sur une IPHostEntry instance.

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

Remarques

La BeginGetHostEntry méthode interroge de façon asynchrone un serveur DNS pour les adresses IP et les alias associés à une adresse IP.

Note Ce membre émet des informations de trace lorsque vous activez le suivi réseau dans votre application. Pour plus d’informations, consultez Network Tracing in the .NET Framework.

L’opération asynchrone BeginGetHostEntry doit être effectuée en appelant la EndGetHostEntry méthode. En règle générale, la méthode est appelée par le requestCallback délégué.

Cette méthode ne bloque pas tant que l’opération n’est pas terminée. Pour bloquer jusqu’à ce que l’opération soit terminée, utilisez la GetHostEntry méthode.

Pour plus d’informations sur l’utilisation du modèle de programmation asynchrone, consultez Appel asynchrone de méthodes synchrones

S’applique à

BeginGetHostEntry(String, AsyncCallback, Object)

Résout de façon asynchrone un nom d’hôte ou une adresse IP en une IPHostEntry instance.

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

Paramètres

hostNameOrAddress
String

Nom d’hôte ou adresse IP à résoudre.

requestCallback
AsyncCallback

Délégué AsyncCallback qui fait référence à la méthode à appeler une fois l’opération terminée.

stateObject
Object

Objet défini par l’utilisateur qui contient des informations sur l’opération. Cet objet est transmis au requestCallback délégué une fois l’opération terminée.

Retours

Instance IAsyncResult qui fait référence à la requête asynchrone.

Exceptions

hostNameOrAddress a la valeur null.

La longueur est hostNameOrAddress supérieure à 255 caractères.

Une erreur est rencontrée lors de la résolution hostNameOrAddress.

hostNameOrAddress est une adresse IP non valide.

Exemples

L’exemple de code suivant utilise la BeginGetHostEntry méthode pour résoudre une adresse IP sur une IPHostEntry instance.

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

Remarques

La BeginGetHostEntry méthode interroge un serveur DNS pour l’adresse IP associée à un nom d’hôte ou une adresse IP.

Note Ce membre émet des informations de trace lorsque vous activez le suivi réseau dans votre application. Pour plus d’informations, consultez Network Tracing in the .NET Framework.

L’opération asynchrone BeginGetHostEntry doit être effectuée en appelant la EndGetHostEntry méthode. En règle générale, la méthode est appelée par le requestCallback délégué.

Cette méthode ne bloque pas tant que l’opération n’est pas terminée. Pour bloquer jusqu’à ce que l’opération soit terminée, utilisez la GetHostEntry méthode.

Pour plus d’informations sur l’utilisation du modèle de programmation asynchrone, consultez Appel asynchrone de méthodes synchrones.

S’applique à