Dns.BeginGetHostEntry Metodo

Definizione

Risolve in modo asincrono un nome host o un indirizzo IP in un'istanza IPHostEntry di .

Overload

Nome Descrizione
BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Risolve in modo asincrono un indirizzo IP in un'istanza IPHostEntry di .

BeginGetHostEntry(String, AsyncCallback, Object)

Risolve in modo asincrono un nome host o un indirizzo IP in un'istanza IPHostEntry di .

BeginGetHostEntry(IPAddress, AsyncCallback, Object)

Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs

Risolve in modo asincrono un indirizzo IP in un'istanza IPHostEntry di .

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

Parametri

address
IPAddress

Indirizzo IP da risolvere.

requestCallback
AsyncCallback

Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione.

stateObject
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione. Questo oggetto viene passato al requestCallback delegato al termine dell'operazione.

Valori restituiti

IAsyncResult Istanza che fa riferimento alla richiesta asincrona.

Eccezioni

address è null.

Si verifica un errore durante la risoluzione di address.

address è un indirizzo IP non valido.

Esempio

Nell'esempio di codice seguente viene usato il BeginGetHostEntry metodo per risolvere un indirizzo IP in un'istanza IPHostEntry di .

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

Commenti

Il BeginGetHostEntry metodo esegue una query asincrona su un server DNS per gli indirizzi IP e gli alias associati a un indirizzo IP.

Nota Questo membro genera informazioni di traccia quando si abilita la traccia di rete nell'applicazione. Per altre informazioni, vedere Network Tracing in the .NET Framework.

L'operazione asincrona BeginGetHostEntry deve essere completata chiamando il EndGetHostEntry metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare il GetHostEntry metodo .

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni

Si applica a

BeginGetHostEntry(String, AsyncCallback, Object)

Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs
Origine:
Dns.cs

Risolve in modo asincrono un nome host o un indirizzo IP in un'istanza IPHostEntry di .

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

Parametri

hostNameOrAddress
String

Nome host o indirizzo IP da risolvere.

requestCallback
AsyncCallback

Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione.

stateObject
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione. Questo oggetto viene passato al requestCallback delegato al termine dell'operazione.

Valori restituiti

IAsyncResult Istanza che fa riferimento alla richiesta asincrona.

Eccezioni

hostNameOrAddress è null.

La lunghezza di hostNameOrAddress è maggiore di 255 caratteri.

Si verifica un errore durante la risoluzione di hostNameOrAddress.

hostNameOrAddress è un indirizzo IP non valido.

Esempio

Nell'esempio di codice seguente viene usato il BeginGetHostEntry metodo per risolvere un indirizzo IP in un'istanza IPHostEntry di .

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

Commenti

Il BeginGetHostEntry metodo esegue una query su un server DNS per l'indirizzo IP associato a un nome host o a un indirizzo IP.

Nota Questo membro genera informazioni di traccia quando si abilita la traccia di rete nell'applicazione. Per altre informazioni, vedere Network Tracing in the .NET Framework.

L'operazione asincrona BeginGetHostEntry deve essere completata chiamando il EndGetHostEntry metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare il GetHostEntry metodo .

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Si applica a