The System.net namespace contains the DNS class, which provides functions required to write DNS. Here are some methods of the DNS class and how to use this class writing programs in C #.
First, synchronous method
Four synchronization methods are defined in the DNS class: gethostname (), gethostbyname (), getHostbyAddress (), resolve (). The following explanation.
1, public static string gethostname ();
Function: Get the host name of the local computer.
Return Value: String, contains the DNS host name of the local computer.
2, public static iphostentry gethostbyname (String hostname)
Function: Get the DNS information specified by the specified DNS host name.
Parameters: String of the DNS name containing the host
Using system;
Using system.net;
Class getDNShostInfo
{
Public static void main (String [] Argv)
{
IF (argv.length! = 1)
{
Console.writeline ("USAGE: GETDNSHOSTINFO HOSTNAME");
Return;
}
IPHOSTENTRY RESULTS = DNS.GETOSTBYNAME (Argv [0]);
Console.writeline ("Host Name: {0}", Results.hostName);
FOREACH (String Alias in Results.Aliases)
{
Console.Writeline ("Alias: {0}", Alias);
}
Foreach (iPaddress Address in Results.AddressList)
{
Console.writeline ("Address: {0}", address.tostring ());
}
}
}
Note: IPHOSTENTRY (Provides container class for Internet Host Address Information) Properties:
AddressList: Gets or sets a list of IP addresses associated with hosts.
Aliases: Get or set the alias list with the host.
Hostname: Gets or sets the DNS name of the host.
3, gethostbyaddress ()
Function: Get the DNS host information of the IP address.
Public Static iphostentry gethostbyaddress (iPaddress);
Public static iphostentry gethostbyaddress (string);
Using system;
Using system.net;
Class GetDnsAddressInfo
{
Public static void main (string) [] Argv)
{
IF (argv.length! = 1)
{
Console.writeline ("USAGE: GETDNSADDRESSINFO Address);
Return;
}
Ipaddress test = ipaddress.parse (Argv [0]);
Iphostentry iphe = dns.gethostbyaddress (TEST);
Console.WriteLine ("Information for {0}", Test.toString ());
Console.WriteLine ("Host Name: {0}", Iphe.hostname;
FOREACH (String alias in iphe.aliases) {
Console.Writeline ("Alias: {0}", Alias);
}
Foreach (iPaddress Address in iPhe.AddressList)
{
Console.writeline ("Address: {0}", address.tostring ());
}
}
}
Note: iPaddress, the Internet Protocol (IP) address is available.
4, public static iphostentry resolve (String hostname)
Function: Work with the DNS host name or IP address to the iPhostentry instance.
Parameters: host name or IP address of DNS style.
Note: The resolve method queries the IP address associated with the host name or IP address in the DNS server. When HostName is the host name of the DNS style and is associated with multiple IP addresses, only the first IP address parsed as the hostname is returned.
Using system;
Using system.net;
Class GetResolveInfo
{
Public static void main (String [] Argv)
{
IF (argv.length! = 1)
{
Console.WriteLine ("Usage: GetResolveInfo Address);
Return;
}
Iphostentry iphe = dns.resolve (argv [0]);
Console.writeLine ("Information for {0}", Argv [0]);
Console.WriteLine ("Host Name: {0}", Iphe.hostname;
Foreach (String Alias in iphe.aliases)
{
Console.Writeline ("Alias: {0}", Alias);
}
Foreach (iPaddress Address in iPhe.AddressList)
{
Console.writeline ("Address: {0}", address.tostring ());
}
}
}
Second, asynchronous method
Four asynchronous methods are defined in DNS: BegingethostByname (), BeginResolve (), EndgetHostbyName (), EndResolve ()
Use example:
Using system;
Using system.drawing;
Using system.net;
Using system.text;
Using system.windows.forms;
Class AsyncResolve form:
{
Textbox address;
Listbox results;
Private asyncCallback onResolved;
Public asyncResolve ()
{
Text = "DNS Address Resolver";
Size = new size (400, 380);
OnResolved = New asyncCallback (resolved);
Label label1 = new label ();
Label1.parent = this;
Label1.text = "Enter Address to resolve:";
Label1.autosize = true;
Label1.Location = New Point (10, 10); address = new textbox ();
Address.Parent = this;
Address.size = new size (200, 2 * font.height);
Address.Location = New Point (10, 35);
Results = new Listbox ();
Results.parent = this;
Results.Location = New Point (10, 65);
Results.size = new size (350, 20 * font.height);
Button Checkit = New Button ();
Checkit.parent = this;
Checkit.text = "resolve";
Checkit.Location = New Point (235, 32);
Checkit.size = new size (7 * font.height, 2 * font.height);
Checkit.Click = New EventHandler (ButtonResolveonClick);
}
Void ButtonResolveonClick (Object Obj, Eventargs EA)
{
Results.Items.clear ();
String addr = address.text;
Object State = new object ();
DNS.BEGINRESOLVE (Addr, OnResolved, State);
}
Private Void Resolved (IASYNCRESULT AR)
{
String buffer;
Iphostentry iphe = DNS.Endresolve (AR);
Buffer = "Host Name:" iphe.hostname;
Results.Items.Add (buffer);
Foreach (String Alias in iphe.aliases)
{
Buffer = "alias:" alias;
Results.Items.Add (buffer);
}
Foreach (iPaddress AddRS in iPhe.AddressList)
{
Buffer = "Address:" addrs.tostring ();
Results.Items.Add (buffer);
}
}
Public static void main ()
{
Application.run (New asyncResolve ());
}
}