Dns.BeginGetHostEntry Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Resolve assíncronamente um nome de host ou endereço IP para uma IPHostEntry instância.
Sobrecargas
| Name | Description |
|---|---|
| BeginGetHostEntry(IPAddress, AsyncCallback, Object) |
Resolve assíncronamente um endereço IP para uma IPHostEntry instância. |
| BeginGetHostEntry(String, AsyncCallback, Object) |
Resolve assíncronamente um nome de host ou endereço IP para uma IPHostEntry instância. |
BeginGetHostEntry(IPAddress, AsyncCallback, Object)
Resolve assíncronamente um endereço IP para uma IPHostEntry instância.
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
Parâmetros
- address
- IPAddress
O endereço IP a resolver.
- requestCallback
- AsyncCallback
Um AsyncCallback delegado que faz referência ao método a invocar quando a operação estiver completa.
- stateObject
- Object
Um objeto definido pelo utilizador que contém informação sobre a operação. Este objeto é passado ao requestCallback delegado quando a operação está concluída.
Devoluções
Uma IAsyncResult instância que faz referência ao pedido assíncrono.
Exceções
address é null.
Encontra-se um erro ao resolver address.
address é um endereço IP inválido.
Exemplos
O exemplo de código seguinte utiliza o BeginGetHostEntry método para resolver um endereço IP numa IPHostEntry instância.
// 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
Observações
O BeginGetHostEntry método consulta assíncronamente um servidor DNS para obter os endereços IP e aliases associados a um endereço IP.
Nota Este membro emite informação de rastreio quando ativa o rastreio de rede na sua aplicação. Para mais informações, consulte Rastreamento de Rede no .NET Framework.
A operação assíncrona BeginGetHostEntry deve ser concluída chamando o EndGetHostEntry método. Normalmente, o método é invocado pelo requestCallback delegado.
Este método não bloqueia até que a operação esteja completa. Para bloquear até a operação estar concluída, use o GetHostEntry método.
Para informações detalhadas sobre a utilização do modelo de programação assíncrona, veja Chamar Métodos Síncronos Assíncronos
Aplica-se a
BeginGetHostEntry(String, AsyncCallback, Object)
Resolve assíncronamente um nome de host ou endereço IP para uma IPHostEntry instância.
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
Parâmetros
- hostNameOrAddress
- String
O nome do host ou o endereço IP a resolver.
- requestCallback
- AsyncCallback
Um AsyncCallback delegado que faz referência ao método a invocar quando a operação estiver completa.
- stateObject
- Object
Um objeto definido pelo utilizador que contém informação sobre a operação. Este objeto é passado ao requestCallback delegado quando a operação está concluída.
Devoluções
Uma IAsyncResult instância que faz referência ao pedido assíncrono.
Exceções
hostNameOrAddress é null.
O comprimento de hostNameOrAddress é superior a 255 caracteres.
Encontra-se um erro ao resolver hostNameOrAddress.
hostNameOrAddress é um endereço IP inválido.
Exemplos
O exemplo de código seguinte utiliza o BeginGetHostEntry método para resolver um endereço IP numa IPHostEntry instância.
// 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
Observações
O BeginGetHostEntry método consulta um servidor DNS para obter o endereço IP associado a um nome de host ou endereço IP.
Nota Este membro emite informação de rastreio quando ativa o rastreio de rede na sua aplicação. Para mais informações, consulte Rastreamento de Rede no .NET Framework.
A operação assíncrona BeginGetHostEntry deve ser concluída chamando o EndGetHostEntry método. Normalmente, o método é invocado pelo requestCallback delegado.
Este método não bloqueia até que a operação esteja completa. Para bloquear até a operação estar concluída, use o GetHostEntry método.
Para informações detalhadas sobre a utilização do modelo de programação assíncrona, veja Chamar Métodos Síncronos Assíncronos.