System.Net.Dns Class

public sealed class Dns

Base Types

Object
  Dns

Assembly

System

Library

Networking

Summary

Obtains domain information from the Domain Name System as defined by IETF RFC 1035 and RFC 1036.

Description

The Dns class creates and sends queries to obtain information about a host server from the Internet Domain Name System (DNS). In order to access DNS, the machine executing the query is required to be connected to a network. If the query is executed on a machine that does not have access to a domain name server, a SocketException is thrown.

Information from the DNS query is returned in an instance of the IPHostEntry class. If the specified host has more than one entry in the DNS database, the IPHostEntry instance contains multiple IP addresses and aliases.

[Note: See the IPHostEntry class page for an example that uses the Dns class.]

See Also

System.Net Namespace

Members

Dns Methods

Dns.BeginGetHostByName Method
Dns.BeginResolve Method
Dns.EndGetHostByName Method
Dns.EndResolve Method
Dns.GetHostByAddress(System.String) Method
Dns.GetHostByAddress(System.Net.IPAddress) Method
Dns.GetHostByName Method
Dns.GetHostName Method
Dns.Resolve Method


Dns.BeginGetHostByName Method

public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object stateObject);

Summary

Begins the asynchronous execution of a DNS query to obtain address information about the specified host.

Parameters

hostName
A String containing the DNS name of the host.
requestCallback
A AsyncCallback delegate, or null .
stateObject
An application-defined object, or null .

Return Value

A IAsyncResult instance that contains information about the asynchronous operation.

Exceptions

Exception TypeCondition
ArgumentNullExceptionhostName is null .
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

Description

This method starts an asynchronous request for information about the specified host. To retrieve the results of the query and release resources allocated by this method, call the System.Net.Dns.EndGetHostByName(System.IAsyncResult) method and specify the IAsyncResult object returned by this method. [Note: The System.Net.Dns.EndGetHostByName(System.IAsyncResult) method should be called exactly once for each call to System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) .]

If the requestCallback parameter is not null , the method referenced by requestCallback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by requestCallback . The method referenced by requestCallback can retrieve the results of the query by calling System.Net.Dns.EndGetHostByName(System.IAsyncResult) .

The stateObject parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.BeginResolve Method

public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject);

Summary

Begins the asynchronous execution of a DNS query to resolve a host name or IP address.

Parameters

hostName
A String containing the DNS name or IP address of the host.
requestCallback
A AsyncCallback delegate or null .
stateObject
An application-defined object, or null .

Return Value

A IAsyncResult instance that contains information about the asynchronous operation.

Exceptions

Exception TypeCondition
ArgumentNullExceptionhostName is null .
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

Description

This method starts an asynchronous request for DNS information about the specified host. To retrieve the results of the query and release resources allocated by this method, call the System.Net.Dns.EndResolve(System.IAsyncResult) method, and specify the IAsyncResult object returned by this method. [Note: The System.Net.Dns.EndResolve(System.IAsyncResult) method should be called exactly once for each call to System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object)]

.

If the requestCallback parameter is not null , the method referenced by requestCallback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by requestCallback. The method referenced by requestCallback can retrieve the results of the query by calling System.Net.Dns.EndResolve(System.IAsyncResult).

The stateObject parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.EndGetHostByName Method

public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult);

Summary

Ends an asynchronous query to obtain address information about the specified host.

Parameters

asyncResult
The IAsyncResult object that holds the state information for the asynchronous operation.

Return Value

A IPHostEntry object containing DNS address information about a host.

Exceptions

Exception TypeCondition
ArgumentNullExceptionasyncResult is null .

Description

This method blocks if the asynchronous operation has not completed.

The System.Net.Dns.EndGetHostByName(System.IAsyncResult) method completes an asynchronous request for DNS information that was started with a call to System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object). The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) method call that began the request.

If the System.Net.Dns.EndGetHostByName(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.EndResolve Method

public static IPHostEntry EndResolve(IAsyncResult asyncResult);

Summary

Ends an asynchronous query to resolve a host name or IP address.

Parameters

asyncResult
The System.IAsyncResult object that holds the state information for the asynchronous operation.

Return Value

A IPHostEntry object containing address information about a host.

Exceptions

Exception TypeCondition
ArgumentNullExceptionasyncResult is null .

Description

This method blocks if the asynchronous operation has not completed.

The System.Net.Dns.EndResolve(System.IAsyncResult) method completes an asynchronous request for DNS information that was started with a call to System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) . The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) method call that began the request.

If the System.Net.Dns.EndResolve(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.GetHostByAddress(System.String) Method

public static IPHostEntry GetHostByAddress(string address);

Summary

Queries DNS for the DNS host name of the specified IP address.

Parameters

address
A String containing an IP address.

Return Value

A IPHostEntry instance containing the host information.

Exceptions

Exception TypeCondition
ArgumentNullExceptionaddress is null .
FormatExceptionaddress is not a valid IP address.
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

Description

The IP address specified by the address parameter is required to be in dotted-quad notation (for example, "192.168.1.2").

See Also

System.Net.Dns Class, System.Net Namespace

Dns.GetHostByAddress(System.Net.IPAddress) Method

public static IPHostEntry GetHostByAddress(IPAddress address);

Summary

Queries DNS for the DNS host name of the specified IP address.

Parameters

address
A IPAddress instance.

Return Value

A IPHostEntry instance containing the host information.

Exceptions

Exception TypeCondition
ArgumentNullExceptionaddress is null .
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.GetHostByName Method

public static IPHostEntry GetHostByName(string hostName);

Summary

Queries DNS for address information about the specified host.

Parameters

hostName
A String containing the DNS name of the host.

Return Value

A IPHostEntry object containing host information for the address specified in hostName .

Exceptions

Exception TypeCondition
ArgumentNullExceptionhostName is null .
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

Description

[Note: To retrieve host information asynchronously, use the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) and System.Net.Dns.EndGetHostByName(System.IAsyncResult) methods.

See the IPHostEntry class page for an example that uses the System.Net.Dns.GetHostByName(System.String) method.

]

See Also

System.Net.Dns Class, System.Net Namespace

Dns.GetHostName Method

public static string GetHostName();

Summary

Gets the DNS host name of the local machine.

Return Value

A String containing the DNS host name of the local machine.

Exceptions

Exception TypeCondition
SocketExceptionAn error was encountered resolving the local host name.
SecurityExceptionThe caller does not have permission to access DNS information.

See Also

System.Net.Dns Class, System.Net Namespace

Dns.Resolve Method

public static IPHostEntry Resolve(string hostName);

Summary

Resolves a DNS host name or IP address to a IPHostEntry instance.

Parameters

hostName
A String containing a DNS-style host name or IP address.

Return Value

A IPHostEntry instance containing address information about the host specified in hostName .

Exceptions

Exception TypeCondition
ArgumentNullExceptionhostName is null .
SocketExceptionAn error was encountered executing the DNS query .
SecurityExceptionThe caller does not have permission to access DNS information.

Description

The System.Net.Dns.Resolve(System.String) method queries a DNS server for the IP address associated with a host name or an IP address in dotted-quad notation.

When hostName is a DNS-style host name associated with multiple IP addresses, only the first IP address that resolves to that host name is returned.

Example

The following example demonstrates the use of the System.Net.Dns.Resolve(System.String) method.

using System;
using System.Net;

public class DnsTest {
 public static void Main() {
   IPHostEntry hostInfo1 = Dns.Resolve("www.contoso.com");
   DisplayHostInfo(hostInfo1);
   Console.WriteLine();
 }

 public static void DisplayHostInfo(IPHostEntry hostInfo) {
   string[] aliases = hostInfo.Aliases;
   IPAddress[] addresses = hostInfo.AddressList;
   Console.WriteLine("The host name is: {0}", hostInfo.HostName);

   for(int x = 0; x < aliases.Length; x++)
     Console.WriteLine("Alias {0} == {1}", aliases[x], addresses[x]);
 }
}
The output is

The host name is: contoso.com

Alias www.contoso.com == 207.46.230.186

See Also

System.Net.Dns Class, System.Net Namespace